Permalink
Browse files

more content

  • Loading branch information...
1 parent b575f50 commit 1e6dc33d14ad036d368b81aff52ac42c164e1739 @tebeka tebeka committed Mar 20, 2013
View
@@ -0,0 +1,65 @@
+import "C"
+Calling C From Go
+20 Mar 2013
+Tags: go c
+
+Miki Tebeka
+Technical Lead, Adconion
+miki.tebeka@gmail.com
+http://mikitebeka.com
+@tebeka
+
+* Why C?
+
+After all we have Go, which is much better :)
+
+However, there is a *huge* volume of software written in C.
+We'd like to re-use it and not re-write it.
+
+
+* Minimal Example
+
+.play import-c/sqrt.go
+
+
+* Strings I
+
+.play import-c/str.go
+
+* Ahhhhhh!
+
+.image import-c/picard1.jpg 400 600
+
+* Strings II
+
+.play import-c/str2.go
+
+* But ...
+
+.image import-c/picard2.jpg 400 600
+
+* Strings III
+
+.play import-c/str3.go
+
+* Structs
+
+.play import-c/struct.go
+
+* Case Study - snowball
+
+
+
+* Problems
+
+- Hard to find docs (see reference slide)
+- Pay attention to memory leaks
+- Compilation time grows
+
+* References
+
+- [[http://golang.org/cmd/cgo/][cgo docs]]
+- [[http://golang.org/doc/articles/c_go_cgo.html][C? Go? Cgo!]]
+- [[http://golang.org/misc/cgo/][cgo examples]]
+- [[https://code.google.com/p/go-wiki/wiki/cgo][cgo wiki]]
+- [[http://stackoverflow.com/search?q=%5Bgo%5D+cgo][StackOverflow]]
@@ -1,22 +0,0 @@
-import "C"
-Calling C From Go
-20 Mar 2013
-Tags: go c
-
-Miki Tebeka
-Technical Lead, Adconion
-miki.tebeka@gmail.com
-http://mikitebeka.com
-@tebeka
-
-* Why C?
-
-After all we have Go, which is much better :)
-
-However, there is a *huge* volume of software written in C.
-We'd like to re-use it and not re-write it.
-
-
-* Minimal Example
-
-.play sqrt.go
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,15 @@
+package main
+
+// #include <string.h>
+import "C"
+
+import (
+ "fmt"
+)
+
+func main() {
+ str := "Gophers Rock!"
+ dup := C.strdup(C.CString(str))
+ fmt.Printf("dup: %s\n", dup)
+}
+
View
@@ -0,0 +1,17 @@
+package main
+
+// #include <string.h>
+// #include <stdlib.h>
+import "C"
+
+import (
+ "fmt"
+)
+
+func main() {
+ str := "Gophers Rock!"
+ dup := C.strdup(C.CString(str))
+
+ gostr := C.GoString(dup)
+ fmt.Println(gostr)
+}
View
@@ -0,0 +1,21 @@
+package main
+
+// #include <string.h>
+// #include <stdlib.h>
+import "C"
+
+import (
+ "fmt"
+ "unsafe"
+)
+
+func main() {
+ str := "Gophers Rock!"
+ dup := C.strdup(C.CString(str))
+ defer func() {
+ C.free(unsafe.Pointer(dup))
+ }()
+
+ gostr := C.GoString(dup)
+ fmt.Println(gostr)
+}
View
@@ -0,0 +1,23 @@
+package main
+
+// meanwhile at stdlib.h:
+// typedef struct
+// {
+// int quot; /* Quotient. */
+// int rem; /* Remainder. */
+// } div_t;
+
+// #include <stdlib.h>
+import "C"
+
+import (
+ "fmt"
+)
+
+func main() {
+ var dt C.div_t
+
+ dt = C.div(16, 6)
+ fmt.Printf("quot: %d, rem: %d\n", dt.quot, dt.rem)
+}
+

0 comments on commit 1e6dc33

Please sign in to comment.