Thanks your reply, I also used this way, but it still not work. code as the following.
gonameunits := []string{"gpu0", "gpu1", "gpu2", "gpu3"} nameunits := make([]*C.char, len(gonameunits)) for i, _ := range gonameunits { nameunits[i] = C.CString(gonameunits[i]) defer C.free(unsafe.Pointer(nameunits[i])) } fmt.Println("nameunits:", nameunits) golevelmatrix := [][]int{{1}, {3, 3}, {3, 3, 2}} levelmatrix := make([][]C.int, len(golevelmatrix)) for i, _ := range golevelmatrix { levelmatrix[i] = make([]C.int, len(golevelmatrix[i])) for j, _ := range golevelmatrix[i] { levelmatrix[i][j] = C.int(golevelmatrix[i][j]) } } fmt.Println("levelmatrix:", levelmatrix) C.test_settopologyresource(mod, C.CString("node1"), C.int(2), (**C.char )(unsafe.Pointer(&nameunits[0])), (**C.int)(unsafe.Pointer(&levelmatrix[0][0 ]))) 在 2017年8月4日星期五 UTC+8下午8:09:34,Konstantin Khomoutov写道: > > On Fri, Aug 04, 2017 at 02:09:14AM -0700, jianzh...@gmail.com > <javascript:> wrote: > > > Hey Guys, > > > > I'm in trouble in the same issue. My code as the following: > > > > test.go > > ```go > > name := []string{"gpu0", "gpu1", "gpu2", "gpu3"} > > matrix := [3][3]int{{1, 0, 0}, {3, 3, 0}, {3, 3, 2}} > > > > C.test_settopologyresource(mod, C.CString("node1"), C.int(2), > > (**C.char)(unsafe.Pointer(&name[0])), > > (**C.int)(unsafe.Pointer(&matrix[0][0]))) > > ``` > > I'm afraid that's not going to work: the elements of the "name" slice > are strings, and they can't be coersed to *C.char because a Go string is > internally a struct consisting of a pointer and a length. > > So I think you'd need to make a "clone" data struct -- something like > this: > > cnames := make([]*C.Char, len(names)) > for i, name := range names { > cnames[i] = C.CString(name) > } > > and then destroy those objects after returning from the C side: > > for _, p := range cnames { > C.free(p) > } > > [...] > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.