Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: f2e50fba50
...
compare: 09207d633e
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
10 2013/import-c.slide
@@ -20,14 +20,16 @@ We'd like to re-use it and not re-write it.
Provides an easy bridge between Go and C
-Problems
+The Downside
- Hard to find docs (see reference slide)
- Pay attention to memory leaks
- Compilation time grows
+- For C++ you need `extern`"C"` and C like wrapper
Note that are other options (such as [[http://www.swig.org/][swig]])
+
* Minimal Example
.play import-c/sqrt.go
@@ -73,6 +75,10 @@ Note that are other options (such as [[http://www.swig.org/][swig]])
.code import-c/callback/callback.go
+* Go Style Errors
+
+.play import-c/err.go
+
* Case Study - snowball
- [[https://bitbucket.org/tebeka/snowball]]
@@ -99,7 +105,7 @@ snowball uses `sb_symbol*` for string
* snowball - List
-The list of languages is a static char* array with a `NULL` sentinal
+The list of languages is a `static`char*` array with a `NULL` sentinal
.code import-c/snowball.go /START_LIST/,/END_LIST/
View
17 2013/import-c/err.go
@@ -0,0 +1,17 @@
+package main
+
+// #include <math.h>
+// #cgo LDFLAGS: -lm
+import "C"
+
+import (
+ "fmt"
+)
+
+func main() {
+ i, err := C.sqrt(-1)
+ if err != nil {
+ fmt.Printf("error: %s\n", err)
+ }
+ fmt.Printf("i = %v\n", i)
+}
View
16 2013/import-c/str3.go
@@ -3,18 +3,20 @@ package main
// #include <string.h>
// #include <stdlib.h>
import "C"
+import "fmt"
+import "unsafe"
-import (
- "fmt"
- "unsafe"
-)
+func free(cstr *C.char) {
+ C.free(unsafe.Pointer(cstr))
+}
func main() {
str := "Gophers Rock!"
+ cstr:= C.CString(str)
+ defer free(cstr)
+
dup := C.strdup(C.CString(str))
- defer func() {
- C.free(unsafe.Pointer(dup))
- }()
+ defer free(dup)
gostr := C.GoString(dup)
fmt.Println(gostr)
View
14 2013/import-c/struct.go
@@ -3,21 +3,21 @@ package main
// meanwhile at stdlib.h:
// typedef struct
// {
-// int quot; /* Quotient. */
-// int rem; /* Remainder. */
+// int quot; /* Quotient. */
+// int rem; /* Remainder. */
// } div_t;
// #include <stdlib.h>
import "C"
-
-import (
- "fmt"
-)
+import "fmt"
func main() {
- var dt C.div_t
+ var dt C.div_t // Look Ma! C types
dt = C.div(16, 6)
fmt.Printf("quot: %d, rem: %d\n", dt.quot, dt.rem)
+
+ dt.quot = 17 // Mutate the struct
+ fmt.Printf("%v\n", dt) // %v works as well
}

No commit comments for this range

Something went wrong with that request. Please try again.