github.com/jwijenbergh/purego@v0.0.0-20240126093400-70ff3a61df13/examples/libc/main.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
     3  
     4  //go:build darwin || freebsd || linux || windows
     5  
     6  package main
     7  
     8  import (
     9  	"fmt"
    10  	"runtime"
    11  
    12  	"github.com/jwijenbergh/purego"
    13  )
    14  
    15  func getSystemLibrary() string {
    16  	switch runtime.GOOS {
    17  	case "darwin":
    18  		return "/usr/lib/libSystem.B.dylib"
    19  	case "linux":
    20  		return "libc.so.6"
    21  	case "freebsd":
    22  		return "libc.so.7"
    23  	case "windows":
    24  		return "ucrtbase.dll"
    25  	default:
    26  		panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
    27  	}
    28  }
    29  
    30  func main() {
    31  	libc, err := openLibrary(getSystemLibrary())
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  	var puts func(string)
    36  	purego.RegisterLibFunc(&puts, libc, "puts")
    37  	puts("Calling C from Go without Cgo!")
    38  }