README.md: add simple example (#93)
- commit
- dd326230d879773252f132011f8385d70cd66e2d
- author
- TotallyGamerJet · · 2023-02-12 18:09 UTC
- committer
- GitHub · · 2023-02-12 18:09 UTC
- parent
- 28b3b71
- signature
- GPG Unverified
4AEE18F83AFDEB23The key is unknown - refs
- tag: v0.2.0-alpha.1
1 file · +36 -0
M README.md +36 -0
| @@ -3,6 +3,42 @@ | |||
| 3 | 3 | ||
| 4 | 4 | A library for calling C functions from Go without Cgo. | |
| 5 | 5 | ||
| 6 | + | ## Example | |
| 7 | + | ||
| 8 | + | ```go | |
| 9 | + | package main | |
| 10 | + | ||
| 11 | + | import ( | |
| 12 | + | "fmt" | |
| 13 | + | "runtime" | |
| 14 | + | ||
| 15 | + | "github.com/ebitengine/purego" | |
| 16 | + | ) | |
| 17 | + | ||
| 18 | + | func getSystemLibrary() string { | |
| 19 | + | switch runtime.GOOS { | |
| 20 | + | case "darwin": | |
| 21 | + | return "/usr/lib/libSystem.B.dylib" | |
| 22 | + | case "linux": | |
| 23 | + | return "libc.so.6" | |
| 24 | + | default: | |
| 25 | + | panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS)) | |
| 26 | + | } | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | func main() { | |
| 30 | + | libc := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL) | |
| 31 | + | if err := purego.Dlerror(); err != "" { | |
| 32 | + | panic(err) | |
| 33 | + | } | |
| 34 | + | var puts func(string) | |
| 35 | + | purego.RegisterLibFunc(&puts, libc, "puts") | |
| 36 | + | puts("Calling C from from Go without Cgo!") | |
| 37 | + | } | |
| 38 | + | ``` | |
| 39 | + | ||
| 40 | + | Then to run: `CGO_ENABED=0 go run main.go` | |
| 41 | + | ||
| 6 | 42 | ### External Code | |
| 7 | 43 | ||
| 8 | 44 | Purego uses code that originates from the Go runtime. These files are under the BSD-3 | |
| @@ -3,6 +3,42 @@ | |||||
| 3 | 3 | ||||
| 4 | A library for calling C functions from Go without Cgo. | 4 | A library for calling C functions from Go without Cgo. | ||
| 5 | 5 | ||||
| 6 | + | ## Example | |||
| 7 | + | ||||
| 8 | + | ```go | |||
| 9 | + | package main | |||
| 10 | + | ||||
| 11 | + | import ( | |||
| 12 | + | "fmt" | |||
| 13 | + | "runtime" | |||
| 14 | + | ||||
| 15 | + | "github.com/ebitengine/purego" | |||
| 16 | + | ) | |||
| 17 | + | ||||
| 18 | + | func getSystemLibrary() string { | |||
| 19 | + | switch runtime.GOOS { | |||
| 20 | + | case "darwin": | |||
| 21 | + | return "/usr/lib/libSystem.B.dylib" | |||
| 22 | + | case "linux": | |||
| 23 | + | return "libc.so.6" | |||
| 24 | + | default: | |||
| 25 | + | panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS)) | |||
| 26 | + | } | |||
| 27 | + | } | |||
| 28 | + | ||||
| 29 | + | func main() { | |||
| 30 | + | libc := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL) | |||
| 31 | + | if err := purego.Dlerror(); err != "" { | |||
| 32 | + | panic(err) | |||
| 33 | + | } | |||
| 34 | + | var puts func(string) | |||
| 35 | + | purego.RegisterLibFunc(&puts, libc, "puts") | |||
| 36 | + | puts("Calling C from from Go without Cgo!") | |||
| 37 | + | } | |||
| 38 | + | ``` | |||
| 39 | + | ||||
| 40 | + | Then to run: `CGO_ENABED=0 go run main.go` | |||
| 41 | + | ||||
| 6 | ### External Code | 42 | ### External Code | ||
| 7 | 43 | ||||
| 8 | Purego uses code that originates from the Go runtime. These files are under the BSD-3 | 44 | Purego uses code that originates from the Go runtime. These files are under the BSD-3 | ||