转自http://www.cnblogs.com/moodlxs/p/4133121.html
虽然golang是用C实现的,并且被称为下一代的C语言,但是golang跟C的差别还是很大的。它定义了一套很丰富的数据类型及数据结构,这些类型和结构或者是直接映射为C的数据类型,或者是用C struct来实现。了解golang的数据类型和数据结构的底层实现,将有助于我们更好的理解golang并写出质量更好的代码。
基础类型
源码在:$GOROOT/src/pkg/runtime/runtime.h 。我们先来看下基础类型:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* *basictypes */ typedefsignedcharint8; typedefunsignedcharuint8; typedefsignedshortint16; typedefunsignedshortuint16; typedefsignedintint32; typedefunsignedintuint32; typedefsignedlonglongintint64; typedefunsignedlonglongintuint64; typedeffloatfloat32; typedefdoublefloat64; #ifdef_64BIT typedefuint64uintptr; typedefint64intptr; typedefint64intgo;//Go'sint typedefuint64uintgo;//Go'suint #else typedefuint32uintptr; typedefint32intptr; typedefint32intgo;//Go'sint typedefuint32uintgo;//Go'suint #endif /* *definedtypes */ typedef uint8bool; typedef uint8 byte; |
1 2 3 4 5 6 7 8 9 10 11 |
packagemain import( "fmt" "reflect" ) funcmain(){ varbbyte='D' fmt.Printf("output:%v\n",reflect.TypeOf(b).Kind()) } |
1 2 3 4 |
$cd$GOPATH/src/basictype_test $gobuild $./basictype_test output:uint8 |
1 2 3 4 5 |
structString { byte*str; intgolen; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
packagemain import( "fmt" "unsafe" ) funcmain(){ varstrstring="hi,陈一回~" p:=(*struct{ struintptr lenint })(unsafe.Pointer(&str)) fmt.Printf("%+v\n",p) } |
1 2 3 4 |
$cd$GOPATH/src/basictype_test $gobuild $./basictype_test output:&{str:135100456len:14} |
1 2 3 4 5 6 |
struct Slice {//mustnotmoveanything byte* array;//actualdata uintgo len;//numberofelements uintgo cap;//allocatednumberofelements }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
packagemain import( "fmt" "unsafe" ) funcmain(){ varslice[]int32=make([]int32,5,10) p:=(*struct{ arrayuintptr lenint capint })(unsafe.Pointer(&slice)) fmt.Printf("output:%+v\n",239)"> |
1 2 3 4 |
$cd$GOPATH/src/basictype_test $gobuild $./basictype_test output:&{array:406958176len:5cap:10} |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
packagemain import( "fmt" ) funcmain(){ vararray=[...]int32{1,2,3,4,5} varslice=array[2:4] fmt.Printf("改变slice之前:array=%+v,slice=%+v\n",array,slice) slice[0]=234 fmt.Printf("改变slice之后:array=%+v,slice) } |
1 2 3 4 5 |
$cd$GOPATH/src/basictype_test $gobuild $./basictype_test 改变slice之前:array=[12345],slice=[34] 改变slice之后:array=[1223445],slice=[2344] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
packagemain import( "fmt" ) funcmain(){ vararray=[...]int32{1,5} varslice=array[2:4] slice=append(slice,6,7,8) fmt.Printf("改变slice之前:array=%+v,slice=[34678] 改变slice之后:array=[12345],slice=[2344678] |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
packagemain import( "fmt" ) funcmain(){ varslice1=[]int32{1,5} varslice2=slice1[2:4] fmt.Printf("改变slice2之前:slice1=%+v,slice2=%+v\n",slice1,slice2) slice2[0]=234 fmt.Printf("改变slice2之后:slice1=%+v,slice2) } |
1 2 3 4 5 |
$cd$GOPATH/src/basictype_test $gobuild $./basictype_test 改变slice2之前:slice1=[12345],slice2=[34] 改变slice2之后:slice1=[1223445],slice2=[2344] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
structType { uintptrsize; uint32hash; uint8_unused; uint8align; uint8fieldAlign; uint8kind; Alg*alg; void*gc; String*string; UncommonType*x; Type*ptrto; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
structIface { Itab* tab; void* data; }; structEface { Type* type; void* data; }; struct Itab { InterfaceType* inter; Type* type; Itab* link; int32 bad; int32 unused; void (*fun[])(void); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
packagemain import( "fmt" "unsafe" ) funcmain(){ varstrinterface{}="HelloWorld!" p:=(*struct{ tabuintptr datauintptr })(unsafe.Pointer(&str)) fmt.Printf("%+v\n",239)"> |
1 2 3 4 |
$cd$GOPATH/src/basictype_test $gobuild $./basictype_test output:&{tab:134966528data:406847688} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
structHmap { uintgocount; uint32flags; uint32hash0; uint8B; uint8keysize; uint8valuesize; uint16bucketsize; byte*buckets; byte*oldbuckets; uintptrnevacuate; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
packagemain import( "fmt" "unsafe" ) funcmain(){ varm=make(map[string]int32,10) m["hello"]=123 p:=(*struct{ countint flagsuint32 hash0uint32 Buint8 keysizeuint8 valuesizeuint8 bucketsizeuint16 bucketsuintptr oldbucketsuintptr nevacuateuintptr })(unsafe.Pointer(&m)) fmt.Printf("output:%+v\n",sans-serif; font-size:14px; line-height:21px; width:868px; background-color:rgb(250,239)"> |
1 2 3 4 |
$cd$GOPATH/src/basictype_test $gobuild $./basictype_test output:&{count:407032064flags:0hash0:134958144B:192keysize:0valuesize:64bucketsize:30063buckets:540701813oldbuckets:0nevacuate:0} |