FAQ
I'm trying to get the uname of a server through the C api through CGO. I
have

#include <sys/utsname.h>

var utsname C.struct_utsname
C.uname(&utsname)

I'm trying to do string(utsname.sysname), but I'm getting an error
"cannot convert utsname.sysname (type [65]_Ctype_char) to type string".
How can I convert that char[] to a string in Go?

I understand that in this specific case I can just read /proc/version,
etc, but I'm curious how char[] can be converted to string.

Any help would be appreciated.

Thanks!

--
Tech163

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jan Mercl at Feb 16, 2013 at 4:25 pm

    On Sat, Feb 16, 2013 at 5:19 PM, Tech163 wrote:
    I understand that in this specific case I can just read /proc/version, etc,
    but I'm curious how char[] can be converted to string.
    Use the CGO[1] provided helpers:
    A few special functions convert between Go and C types by making copies of the data. In pseudo-Go definitions:

    // Go string to C string
    // The C string is allocated in the C heap using malloc.
    // It is the caller's responsibility to arrange for it to be
    // freed, such as by calling C.free.
    func C.CString(string) *C.char

    // C string to Go string
    func C.GoString(*C.char) string

    // C string, length to Go string
    func C.GoStringN(*C.char, C.int) string

    // C pointer, length to Go []byte
    func C.GoBytes(unsafe.Pointer, C.int) []byte
    [1]: http://golang.org/cmd/cgo/

    -j

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tech163 at Feb 16, 2013 at 6:21 pm
    Thanks.

    C.GoString(&utsname.sysname[0])

    seems to work well.

    Tech163
    On 2/16/13 11:25, Jan Mercl wrote:
    On Sat, Feb 16, 2013 at 5:19 PM, Tech163 wrote:
    I understand that in this specific case I can just read /proc/version, etc,
    but I'm curious how char[] can be converted to string.
    Use the CGO[1] provided helpers:
    A few special functions convert between Go and C types by making copies of the data. In pseudo-Go definitions:

    // Go string to C string
    // The C string is allocated in the C heap using malloc.
    // It is the caller's responsibility to arrange for it to be
    // freed, such as by calling C.free.
    func C.CString(string) *C.char

    // C string to Go string
    func C.GoString(*C.char) string

    // C string, length to Go string
    func C.GoStringN(*C.char, C.int) string

    // C pointer, length to Go []byte
    func C.GoBytes(unsafe.Pointer, C.int) []byte
    [1]: http://golang.org/cmd/cgo/

    -j
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 16, '13 at 4:20p
activeFeb 16, '13 at 6:21p
posts3
users2
websitegolang.org

2 users in discussion

Tech163: 2 posts Jan Mercl: 1 post

People

Translate

site design / logo © 2021 Grokbase