Skip to content

Instantly share code, notes, and snippets.

@xlab
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save xlab/64cce5bd30fb828d7c72 to your computer and use it in GitHub Desktop.

Select an option

Save xlab/64cce5bd30fb828d7c72 to your computer and use it in GitHub Desktop.
package main
// #include <stdio.h>
//
// typedef struct Box {
// unsigned int width;
// unsigned int height;
// float weight;
// const char *name;
// } Box;
//
// void checkBoxes(Box **box, unsigned int size) {
// for (int i = 0; i < size; i++) {
// printf("got a box %dx%d %flb, contents: '%s'\n", box[i]->width, box[i]->height, box[i]->weight, box[i]->name);
// }
// }
import "C"
import "unsafe"
func main() {
CheckBoxes([]*Box{
{
Width: 10, Height: 5,
Weight: 10.99,
Name: "Cozy lamp",
}, {
Width: 6, Height: 6,
Weight: 5.0,
Name: "Modern lamp",
},
{
Width: 1, Height: 1,
Weight: 0.1,
Name: "A key",
},
})
}
type Box struct {
Width uint32
Height uint32
Weight float32
Name string
}
func CheckBoxes(boxes []*Box) {
C.checkBoxes((**C.struct_Box)(unsafe.Pointer(&boxes[0])), C.uint(len(boxes)))
}
$ go run main.go
got a box 10x5 10.990000lb, contents: 'Cozy lamp'
got a box 6x6 5.000000lb, contents: 'Modern lamp'
got a box 1x1 0.100000lb, contents: 'A key'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment