FAQ
Using CGo, how would I access an array of unsigned ints, when I'm passed
a pointer to an unsigned int?

ie in C I would do this (using the equivalence of arrays and pointers):
for (i = 0; i < arr_len; i++) {
printf(".%u", arr[i]);
}
My Go equivalent is dying as I'm using the wrong syntax:
for i := 0; i < arr_len; i++ {
fmt.Printf(".%d", arr[i])
}

invalid operation: arr[i] (index of type *_Ctype_uint)
(this is for converting an SNMP OID that's in an array of uint's to a
string. I know about C's "unsigned int" being call C.uint in Go).

--
Sonia Hamilton
http://www.snowfrog.net

Search Discussions

  • Andy Balholm at Jan 25, 2013 at 4:33 am
    If you know the length of the array at compile time, cast the pointer to an
    array type:

    arr1 := (*[6]C.uint)arr

    If you don't know the length at compile time, your options would be using
    pointer arithmetic or using runtime.SliceHeader to create a slice.

    --
  • Sonia Hamilton at Jan 25, 2013 at 1:37 pm

    * Andy Balholm [2013-01-24 20:33:42 -0800]:

    If you know the length of the array at compile time, cast the pointer to an
    array type:

    arr1 := (*[6]C.uint)arr

    If you don't know the length at compile time, your options would be using
    pointer arithmetic or using runtime.SliceHeader to create a slice.
    * Kevin Gillette [2013-01-24 19:27:24 -0800]:
    If the length of the C array is invariant, you can use a go array pointer
    value, such as *[30]uint32 and assign the pointer to the C data via
    unsafe.Pointer. If it's variable length, you can construct a []uint32 slice
    header using unsafe and reflect.
    * minux [2013-01-25 18:21:54 +0800]:
    or just use a ridiculously large array length, for example, 1<<32, and then
    slice it.
    Thanks guys. I couldn't get slice conversion working (I was following
    [1]). In the end I used GoBytes() and binary.Read(); any improvements on
    this solution?
    // OidToStr converts an oid from C array of guint32's to a Go string
    func OidToStr(oid *_Ctype_guint32, oid_len _Ctype_gsize) (result string) {
    size := int(unsafe.Sizeof(oid))
    length := int(oid_len)
    gbytes := C.GoBytes(unsafe.Pointer(oid), (_Ctype_int)(size*length))

    buf := bytes.NewBuffer(gbytes)
    for i := 0; i < length; i++ {
    var out uint32
    if err := binary.Read(buf, binary.LittleEndian, &out); err == nil {
    result = result + fmt.Sprintf(".%d", out)
    } else {
    return "<error converting oid>"
    }
    }
    return result[1:]
    }
    [1] https://code.google.com/p/go-wiki/wiki/cgo, "Turning C arrays into
    Go slices"

    --
    Sonia Hamilton
    http://www.snowfrog.net

    --
  • Kevin Gillette at Jan 25, 2013 at 5:04 am
    If the length of the C array is invariant, you can use a go array pointer
    value, such as *[30]uint32 and assign the pointer to the C data via
    unsafe.Pointer. If it's variable length, you can construct a []uint32 slice
    header using unsafe and reflect.
    On Thursday, January 24, 2013 8:03:28 PM UTC-7, Sonia Hamilton wrote:

    Using CGo, how would I access an array of unsigned ints, when I'm passed
    a pointer to an unsigned int?

    ie in C I would do this (using the equivalence of arrays and pointers):
    for (i = 0; i < arr_len; i++) {
    printf(".%u", arr[i]);
    }
    My Go equivalent is dying as I'm using the wrong syntax:
    for i := 0; i < arr_len; i++ {
    fmt.Printf(".%d", arr[i])
    }

    invalid operation: arr[i] (index of type *_Ctype_uint)
    (this is for converting an SNMP OID that's in an array of uint's to a
    string. I know about C's "unsigned int" being call C.uint in Go).

    --
    Sonia Hamilton
    http://www.snowfrog.net
  • Minux at Jan 25, 2013 at 10:22 am

    On Fri, Jan 25, 2013 at 11:27 AM, Kevin Gillette wrote:

    If the length of the C array is invariant, you can use a go array pointer
    value, such as *[30]uint32 and assign the pointer to the C data via
    unsafe.Pointer. If it's variable length, you can construct a []uint32 slice
    header using unsafe and reflect.
    or just use a ridiculously large array length, for example, 1<<32, and then
    slice it.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 25, '13 at 3:03a
activeJan 25, '13 at 1:37p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase