File: main.go

package info (click to toggle)
golang-github-ebitengine-purego 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 844 kB
  • sloc: asm: 10,607; ansic: 610; cpp: 8; makefile: 3
file content (40 lines) | stat: -rw-r--r-- 773 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors

//go:build darwin || freebsd || linux || netbsd || windows

package main

import (
	"fmt"
	"runtime"

	"github.com/ebitengine/purego"
)

func getSystemLibrary() string {
	switch runtime.GOOS {
	case "darwin":
		return "/usr/lib/libSystem.B.dylib"
	case "freebsd":
		return "libc.so.7"
	case "linux":
		return "libc.so.6"
	case "netbsd":
		return "libc.so"
	case "windows":
		return "ucrtbase.dll"
	default:
		panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
	}
}

func main() {
	libc, err := openLibrary(getSystemLibrary())
	if err != nil {
		panic(err)
	}
	var puts func(string)
	purego.RegisterLibFunc(&puts, libc, "puts")
	puts("Calling C from Go without Cgo!")
}