0

I have the following C code

uint32_t cHash32(const char *s, size_t len) { return util::Hash32(s, len); }

I am calling it from a go project as follows

func Hash32(s []byte) uint32 {
    return uint32(C.cHash32((*C.char)(unsafe.Pointer(&s)),   C.size_t(len(s))))
}

Somehow the result is broken.

When passing "hi" the expected result should be 4063302914 according to the python bindings to the same library (farmhash by google).

I guess that assuming s can be translated to a *C.char is a bit naive isn't it!

How do I pass the content of s as a *C.char?

Seif Lotfy
  • 109
  • 2
  • 5

1 Answers1

3

Yes, you can create *C.char in Go via C.CString. cgo is not allowed on google playground, so you will need to download this link and run it locally to use it.

http://play.golang.org/p/inthA1i0C2

Convert the byte slice to a string, then to *char C.CString(string([]byte("bytes"))).

Drew
  • 4,683
  • 4
  • 35
  • 50