2017. november 6., hétfő 2:11:48 UTC+1 időpontban Chun Zhang a következőt írta: > > Hi, All, > > I am trying to read a configuration file using Viper, the config file is a > very simple json file with one line > { > "device" : "eth1" > } > > I use the following line to read it > > device := viper.GetString("device") > > > then passing this var further into a C library with swig generated api as > arg2 > > itc := device //Need to update if there is more devices > libpi.PI_init_global_config(1, &itc) > > func PI_init_global_config(arg1 int, arg2 *string) { > > > > I can retrieve the correct value, and can verify that var device does > have eth1 as the value when printf it. I even did the compare of device == > "eth1", and the result is true. > > However, when passing this device var to the PI_init_gloable_config API, I > got code crashed with the following error > > *panic: runtime error: cgo argument has Go pointer to Go pointer* > >
The value you pass to C must not have a pointer which points to a Go pointer. What are you passing to libpi.PI_init_global_config ? I think "device" is already a *string, so &itc is a **string - try libpi.PI_init_global_config(device). (I assume libpi does some conversion, as the proper way to pass a string ot C (*char) is to convert the string to *char with cs:=C.CString(*device) and later C.free(unsafe.Pointer(cs)) it). > > If instead of reading this file from configuration json, I hardcode it in > the code as > > device := "eth1" > > then there is no issue at all. If I read this variable using Flag from > command line, like > > flag.StringVar(&device, "Device", "eth1", "NIC to Listen") > > > there was no problem either. > > > > Can anybody please enlighten me what's the difference? Why this error is > triggered? I googled quite a bit, but it does not seem to help. I have go > 1.8.3 installed. > > Thanks, > Chun > -- 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.