Hi Jusitn, I realised I have value_len field in my arguments.I did this :
func Nitish(key *C.char, value *C.char, value_len C.size_t){ f.WriteString(C.GoString(key)+ ":") s := C.GoStringN(value, C.int(value_len)) //fmt.Println(s) f.WriteString(s) f.WriteString("\n") } Am I doing it correctly? Thanks On Wed, May 1, 2019 at 4:40 PM Nitish Saboo <nitish.sabo...@gmail.com> wrote: > Hi Justin, > > As you had suggested: > **************************************** > You could use something like this: > https://play.golang.org/p/fcE2UJEUFPy > > That example has the option of only converting a fixed number of bytes > from the C string to the Go string, if you know how big the prefix is. > ************************************************* > > > I made the following changes to my function: > > func Nitish(key *C.char, value *C.char, value_len C.size_t){ > > f.WriteString(C.GoString(key)+ ":") > value1 := C.CString(C.GoString(value)) > value_len1 := C.size_t(value1) > // If we know the max size of characters > // that we are looking for, then we can > // limit how many we need to convert from > // the C string > const maxLen = 40 > if value_len1 > maxLen { > value_len1 = C.size_t(maxLen) > } > > s := C.GoStringN(value, C.int(value_len1)) > //fmt.Println(s) > > parts := strings.SplitN(s, " ", 2) > //fmt.Println(parts[0]) > f.WriteString(parts[0]) > f.WriteString("\n") > > } > > *value *field is pointer to strings in C.I am getting the following error: > > # command-line-arguments > ./main.go:109:24: cannot convert value1 (type *_Ctype_char) to type > _Ctype_ulong > makefile:6: recipe for target 'main' failed > make: *** [main] Error 2 > > I am new to cgo.I know I am making a silly mistake to fetch the value of > value1.Can you please point it out ? > > Thanks, > Nitish > > On Wed, May 1, 2019 at 3:10 PM Nitish Saboo <nitish.sabo...@gmail.com> > wrote: > >> Actually, this is what is happening: >> >> func Nitish(key *C.char, value *C.char, value_len C.size_t){ >> >> f.WriteString(C.GoString(key)+ ":") >> f.WriteString(C.GoString(value)) >> f.WriteString("\n") >> >> } >> >> This callback method is getting called from C code multiple times. >> >> I am planning to write the key, value pairs in a file.But the 'value' field >> is getting appended with the previous value in the buffer. >> >> These are the contents that are getting written to the file.Please see the >> following from bottom to top.If you see for every key field (bottom to top), >> image/gif and then 'Fp5PpR2roT6uPnte47' is getting appended as I continue >> writing key-values to the file. >> >> But I need only the first word from the value field. >> >> Actual: >> >> sentfileid: - Fp5PpR2roT6uPnte47 image/gif >> sentmimetype: Fp5PpR2roT6uPnte47 image/gif >> rcvdfileid: image/gif >> >> Expected: >> >> sentfileid: - >> sentmimetype: Fp5PpR2roT6uPnte47 >> rcvdfileid: image/gif >> >> Please let me know how can I achieve this ? >> >> Thanks >> >> >> >> On Wed, May 1, 2019 at 11:25 AM Nitish Saboo <nitish.sabo...@gmail.com> >> wrote: >> >>> Hi, >>> I have this method: >>> >>> func CallBack(key *C.char, value *C.char, value_len C.size_t){ >>> >>> } >>> >>> and the *value* parameter in the arguments is containing the given >>> string 'Fp5PpR2roT6uPnte47 image/gif'. >>> I want to extract only first word from the string.Let me know how can I >>> do this. >>> >>> I don't want to split the string into a slice of individual characters. >>> >>> Thanks >>> >>> On Wed, May 1, 2019 at 6:27 AM Justin Israel <justinisr...@gmail.com> >>> wrote: >>> >>>> >>>> >>>> On Wednesday, May 1, 2019 at 7:06:47 AM UTC+12, Nitish Saboo wrote: >>>>> >>>>> Apologies.I did this 'strings.Split(C.GoString(*C.char),""). >>>>> I am getting 'Incompatible types' compilation error. >>>>> >>>> >>>> Are you literally doing this? >>>> strings.Split(C.GoString(*C.char),"") >>>> >>>> Something like this should be working correctly: >>>> >>>> someCArr := getSomeCArr() >>>> chars := strings.Split(C.GoString(someCArr),"") >>>> >>>> Is your goal to split the string into a slice of individual characters? >>>> Because that is what your delim suggests. >>>> >>>> >>>>> Thanks >>>>> >>>>> >>>>> On Wed, May 1, 2019 at 12:30 AM Nitish Saboo <nitish...@gmail.com> >>>>> wrote: >>>>> >>>>>> Hi, >>>>>> >>>>>> I want to convert *C.char to array of strings in Go. >>>>>> I want to do this because I want the first word from the >>>>>> string.Strings are getting appended to *C.char and I want the first word >>>>>> of >>>>>> the string. >>>>>> I am doing it using 'strings.Split(C.GoString(*C.char))[0]', but it >>>>>> is giving error. >>>>>> Can someone correct me here ? >>>>>> >>>>>> Thanks >>>>>> >>>>>> -- >>>>>> 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 golan...@googlegroups.com. >>>>>> For more options, visit https://groups.google.com/d/optout. >>>>>> >>>>> -- >>>> 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. >>>> >>> -- 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.