I have the following simple test go code : call_c.go
package main
/*
int getData(int *p, int n )
{
int i;
for(i=0;i<n;i++) {
p[i]=i;
}
return n;
}
*/
import "C"
import "fmt"
func main() {
var a [5]int32
i := C.getData((*C.int)(&a[0]), 5)
fmt.Println(i, a)
}
I use "go build call_c.go" to build the program and that builds fine.
But I found that if I simplely reverse the order of "import fmt", "import C" or add a newline between the C /* */ block code and the first import statement, the "go build call_c.go" command will return an error:
go build call_c.go
37: error: 'getData' undeclared (first use in this function)
Questions for expert gophers out there:
Why is the order of import important in go?
Why can't I add space between C block and the first import statement?
If I see this kind of error again, how best to debug it?