Cgo - Stringer for C enum

523 views
Skip to first unread message

Sonia Hamilton

unread,
Dec 16, 2012, 7:25:13 AM12/16/12
to golan...@googlegroups.com
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

Sanjay

unread,
Dec 16, 2012, 9:32:45 AM12/16/12
to golan...@googlegroups.com
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

unread,
Dec 16, 2012, 10:01:10 AM12/16/12
to Sonia Hamilton, golan...@googlegroups.com
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.

Mike Rosset

unread,
Dec 17, 2012, 5:42:21 AM12/17/12
to minux, Sonia Hamilton, golang-nuts
Sonia pretty sure you can use C.CNetSnmpUriType vs the cgo internal
type of _Ctype_GNetSnmpUriType.
> --
>
>

minux

unread,
Dec 17, 2012, 7:38:36 AM12/17/12
to Mike Rosset, Sonia Hamilton, golang-nuts
On Mon, Dec 17, 2012 at 6:42 PM, Mike Rosset <mike....@gmail.com> 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.
Reply all
Reply to author
Forward
0 new messages