FAQ
Using cgo I've implemented Stringer for a C enum.

It works, I'm just wondering if there's a better way than the following?
For an enum with a bazillion members, it would be rather tedious...

type UriType
int



const
(

GNET_SNMP_URI_GET UriType =
iota


GNET_SNMP_URI_NEXT


GNET_SNMP_URI_WALK

)



// Stringer for
_Ctype_GNetSnmpUriType

//

//
/usr/include/gsnmp/utils.h

//

// typedef
enum

//
{

//
GNET_SNMP_URI_GET,

//
GNET_SNMP_URI_NEXT,

//
GNET_SNMP_URI_WALK

// }
GNetSnmpUriType;

func (uritype _Ctype_GNetSnmpUriType) String() string
{
switch UriType(uritype)
{

case
GNET_SNMP_URI_GET:

return
"GNET_SNMP_URI_GET"

case
GNET_SNMP_URI_NEXT:

return
"GNET_SNMP_URI_NEXT"

case
GNET_SNMP_URI_WALK:

return
"GNET_SNMP_URI_WALK"


}

return "UNKNOWN
GNetSnmpUriType"

}

--
Sonia

--

Search Discussions

  • Sanjay at Dec 16, 2012 at 2:32 pm
    I've done this in the past with a global array of strings and have the
    String function just index the array. It reduces the number of lines you
    have to write by 1... but I can't think of anything better without a
    preprocessing step.

    Sanjay

    --
  • Minux at Dec 16, 2012 at 3:01 pm

    On Sun, Dec 16, 2012 at 8:25 PM, Sonia Hamilton wrote:

    Using cgo I've implemented Stringer for a C enum.

    It works, I'm just wondering if there's a better way than the following?
    For an enum with a bazillion members, it would be rather tedious...

    type UriType
    int



    const
    (

    GNET_SNMP_URI_GET UriType =
    iota


    GNET_SNMP_URI_NEXT


    GNET_SNMP_URI_WALK

    )



    // Stringer for
    _Ctype_GNetSnmpUriType

    //

    //
    /usr/include/gsnmp/utils.h

    //

    // typedef
    enum

    //
    {

    //
    GNET_SNMP_URI_GET,

    //
    GNET_SNMP_URI_NEXT,

    //
    GNET_SNMP_URI_WALK

    // }
    GNetSnmpUriType;

    func (uritype _Ctype_GNetSnmpUriType) String() string
    {
    switch UriType(uritype)
    {

    case
    GNET_SNMP_URI_GET:

    return
    "GNET_SNMP_URI_GET"

    case
    GNET_SNMP_URI_NEXT:

    return
    "GNET_SNMP_URI_NEXT"

    case
    GNET_SNMP_URI_WALK:

    return
    "GNET_SNMP_URI_WALK"


    }

    return "UNKNOWN
    GNetSnmpUriType"

    }
    I'd suggest you write a script/Go program to parse and generate the progrom
    for this problem
    and let the computer do the hard work; there is not better approaches other
    than to make an
    array of the strings and slightly shorten the String() method.

    for example, the syscall package uses a perl script driven by
    mkall.sh<http://tip.golang.org/src/pkg/syscall/mkall.sh> to
    generate all kinds of
    constants and even Go syscall interface functions directly from system (or
    even kernel source)
    headers.

    --
  • Mike Rosset at Dec 17, 2012 at 10:42 am
    Sonia pretty sure you can use C.CNetSnmpUriType vs the cgo internal
    type of _Ctype_GNetSnmpUriType.
    On Sun, Dec 16, 2012 at 7:01 AM, minux wrote:
    On Sun, Dec 16, 2012 at 8:25 PM, Sonia Hamilton wrote:

    Using cgo I've implemented Stringer for a C enum.

    It works, I'm just wondering if there's a better way than the following?
    For an enum with a bazillion members, it would be rather tedious...

    type UriType int

    const (
    GNET_SNMP_URI_GET UriType = iota
    GNET_SNMP_URI_NEXT
    GNET_SNMP_URI_WALK
    )

    // Stringer for _Ctype_GNetSnmpUriType
    //
    // /usr/include/gsnmp/utils.h
    //
    // typedef enum
    // {
    // GNET_SNMP_URI_GET,
    // GNET_SNMP_URI_NEXT,
    // GNET_SNMP_URI_WALK
    // } GNetSnmpUriType;
    func (uritype _Ctype_GNetSnmpUriType) String() string {
    switch UriType(uritype) {
    case GNET_SNMP_URI_GET:
    return "GNET_SNMP_URI_GET"
    case GNET_SNMP_URI_NEXT:
    return "GNET_SNMP_URI_NEXT"
    case GNET_SNMP_URI_WALK:
    return "GNET_SNMP_URI_WALK"
    }
    return "UNKNOWN GNetSnmpUriType"
    }
    I'd suggest you write a script/Go program to parse and generate the progrom
    for this problem
    and let the computer do the hard work; there is not better approaches other
    than to make an
    array of the strings and slightly shorten the String() method.

    for example, the syscall package uses a perl script driven by mkall.sh to
    generate all kinds of
    constants and even Go syscall interface functions directly from system (or
    even kernel source)
    headers.

    --
    --
  • Minux at Dec 17, 2012 at 12:38 pm

    On Mon, Dec 17, 2012 at 6:42 PM, Mike Rosset wrote:

    Sonia pretty sure you can use C.CNetSnmpUriType vs the cgo internal
    type of _Ctype_GNetSnmpUriType.
    you can't directly attach method to C.XXX type, either you define a new Go
    type alias, or
    use the internal type name.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 16, '12 at 12:31p
activeDec 17, '12 at 12:38p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase