github.com/ebitengine/purego@v0.8.0-alpha.2.0.20240512170805-6cd12240d332/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/ebitengine/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  }