In-memory form
Let's start by looking at how some of the underlying types exist in memory in go, as shown in:
The type of the variable J is int32, and the type of variable i is int, which is not the same type, so the assignment operation i=j is a type error cannot use j (type int32) as type int in assignment .
The right way should be
Int (7)int32 (Int (j)
The domain of the struct is tightly aligned in memory.
Static type and underlying type
Byte is the static type of go, and uint8 is the underlying type of Go
Rune is an alias for Int32, which is used to represent Unicode characters. It usually needs to be used when dealing with Chinese.
String type
Defined ingo-1.8.3/go/src/runtime/string.go
struct { str unsafe. Pointer int}
Two properties: A pointer, an int of length, are private members!
The string type is represented in the memory model of the Go language with a 2 word-length data structure. As you can see, multiple strings are actually shared by one store.
str[i:j]For a string slice operation, a new object is obtained, and the pointer to the type stringStruct struct object still points to the underlying storage of STR, in length j-i . This means that string slicing does not involve memory allocations or copy operations, which are efficient equivalent to passing subscripts.
The built-in Len () operation of the string type takes the Len value directly from the underlying structure without additional action
Slice type
Defined in/go-1.8.3/src/runtime/slice.go
struct {array unsafe. Pointerlen intint}
Obviously, the type slice struct is similar to the type stringstruct struct above, with just one more cap attribute.
A slice is a partial reference to an underlying array. Similarly, slicing the underlying data does not involve memory allocations or copy operations, just a new type slice struct object!
It is important to note that, in the Y[0:4] is valid, the printed result will be[3,5,7,11]
Because Slice is a multi-word structure that differs from pointers, the split operation does not need to allocate memory or even slice headers that are normally stored in the heap. This representation makes the slice operation as inexpensive as passing pointers and length pairs in C.
Slice related functions have the following several, is not feel very familiar.
Makeslice (et *
Expansion of Slice
When you perform a append operation on a slice, you may cause an expansion operation. The expansion rules are as follows:
- If the new size is more than twice times the current size, the size grows to the new size
- Otherwise, loop the following operation: If the current length len is less than 1024, increase by twice times each time, otherwise increase by 1/4 of the current cap each time. Until the size of the growth exceeds or equals the new size.
: = old. cap: = Newcap + newcap//and double for Old.cap compare else {if old. else {4} }}
Slice and unsafe. Pointer Mutual conversion
- Use Make+slice to get the ram out of your own management .... This is a better one.
Make ([]): = unsafe. Pointer (&s[0])
- Based on the memory pointer ptr constructs a slice
o []byte: = (*reflect. (Sliceheader) (unsafe. Pointer (&o))) Sliceheader. Cap = Lengthsliceheader. Len = Lengthsliceheader. uintptr (PTR)
New and make
type slice structOnce you know the definition of go, it's much easier to understand the difference between new and make.
- New (T), which allocates only memory and does not initialize. Returns a
*T value of 0 that points to a type T.
- Make (T, args), allocates memory, and initializes it. Return is
T itself. Because T itself is a reference type.
Use new and make as an example of the type of subordinate declaration:
int} point}Max *point}
Original: https://github.com/Kevin-fqh/learning-k8s-source-code/blob/master/%E6%B7%B1%E5%85%A5go/(%e5%9f%ba%e6%9c%) Ac%e7%b1%bb%e5%9e%8b.md
Go Language Basic type