Go language string data type format

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the go language, define the following string:
var ss string = "12345"

For a programmer who is familiar with C + +, it is immediately thought of what the string is, how much space it is, how memory is allocated, and so on, so let's analyze the problem.

The go language string is a data type that occupies 16 bytes of space, the first 8 bytes is a pointer to the address of the string value, the last eight bytes is an integer, and the length of the string is identified; note that the inside of the go language string does not 0 ' ends with a length field to represent the length of the string.

type mystr struct {    strbuf uintptr;    strlen uint64;}

The above is the type definition of string. Here's a code to verify the problem:

  Package Mainimport ("FMT" "unsafe"//"reflect") type mystr struct {strbuf uintptr; Strlen UInt64;} Func printmemory (P uintptr, size int) {FMT. Printf ("[0x%16x:%2d] =", p, size) for I: = 0; i < size; i++ {p1: = unsafe. Pointer (p + uintptr (i)) P2: = (*byte) (unsafe. Pointer (p1)) fmt. Printf ("%x", *P2)} fmt.    Printf ("\ n")}func main () {var ss string = "12345"; Fmt. Printf ("string=%v\n", ss) Fmt. Printf ("length=%v\n", Len (ss)) Fmt. Printf ("size=%v\n", unsafe. Sizeof (ss)) Fmt. Printf ("address=%v\n", &ss) ptr: = unsafe. Pointer (&SS)//value: = reflect. ValueOf (&SS)//fmt. Println (reflect. TypeOf (value), reflect. ValueOf (value). Kind ())//ptr1: = value.    Pointer () Ptr1: = UIntPtr (PTR) printmemory (PTR1, 16); PTR2: = (* mystr) (PTR) fmt. Printf ("mystr.strbuf=%x\n", Ptr2.strbuf) fmt. Printf ("mystr.strlen=%v\n", Ptr2.strlen) printmemory (Ptr2.strbuf, Len (ss) +1);}  

The results of the implementation are as follows:

$ go build main.go && ./main string=12345length=5size=16address=0xc42000e2c0[0x      c42000e2c0:16] = f1 70 4a 0 0 0 0 0 5 0 0 0 0 0 0 0mystr.strbuf=4a70f1mystr.strlen=5[0x          4a70f1: 6] = 31 32 33 34 35 31

We see the memory structure of the string, which contains a pointer to the string data, and an integer value that identifies the length of the string, and the end of the string does not have a ' s ' to identify, in the example above is a random value (0x31).

Pointer operations on the GO language

The only difference between a go language pointer and a C + + pointer is that the go language does not allow arithmetic operations on pointers (+ 、-、 + + 、--).

However, go provides a set of underlying libraries reflect and unsafe, which can convert any go pointer to a value of type uintptr, and then perform arithmetic operations on the pointer as in C + + and finally revert to the Go type. So from this point of view, the go pointer can also be used as a C + + pointer, but will be compared around, it also requires the user to understand, if you really want to use the pointer, then please remember the consequences of self-esteem.

Here is an example of a Go language pointer operation:
Https://play.golang.org/p/z_GMnh38Z1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.