This is a creation in Article, where the information may have evolved or changed.
Unauthorized use of seven bull Daniel's PPT material, hope not to blame, here thank Daniel Share.
Mapping map
- A "key-value" pair
- Key points: attention to concurrent read and write situation, locked
- Cannot write to uninitialized map, throws exception (panic)
- For iterative operations, each result may be inconsistent (disorderly order)
varMMap[string]intm["K1"] =7 //Error: Assignment to entry in nil mapM: = Make(Map[string]int)//Initialize to an empty map: [], can read and writem["K1"] =7m["K2"] = -Delete(M,"K2")//delete the value corresponding to "K2"V, OK: = m["K2"]//Check if the value of "K2" existsFmt. Println (m["K3"])//non-existent key returns null value by default forK, V: =Rangem {//The results of the iteration may be inconsistent each timeFmt. Println (k, V)}
The make () built-in method can only be used to create slice, map, and channel
Slice, map, channel is a reference to the underlying data structure that must be initialized before use.
Pointer
- Go has pointers, but there is no pointer operation. You cannot traverse the individual bytes of a string with a pointer variable.
- The Go pointer is just a reference.
- The FETCH operator & get the variable storage address, can be assigned to a pointer.
To create your own defined type
type Person struc { string int stringnew"Lishi"// 首字母大写字段,可导出,可在其它包中进行读写"28""He"// 首字母小写字段,不可导出,不可在其它包中进行读写
Method definition
funcstring) { fmt.Println("Study some language...", lang)}p.Study("Golang")
Anonymous fields
typeSstruct{AstringBstring}typeBstruct{S//Anonymous field, only Type S, field name is SBint //Field name is B int //Anonymous field, only type int, meaningless}varb BB.S.A ="a" //Assign a value to the anonymous field SB.A ="a" //Ibid .Fmt. Println (b)//Output result: {{a}}not {a}typeStringstringb.s.b ="B" //Assign a value to the anonymous field SB.B =Ten //Name conflict, different, cannot assign a value to the same field in an anonymous field
Method inheritance
typestruct { string}typestruct { S}func (s *S) String() { fmt.Println("In A: ", s.a, s.b)}var"a""b"// 等价于 b.S.String()
Method overrides
Add a method to type B to overwrite the String in the type S ()
type S struct { a, b string}type B struct { S}func (s *S) String() { fmt.Println("In A: ", s.a, s.b)}func (b *B) String() { fmt.Println("In B: ", b.a, b.b)}var b Bb.a"a"b.b"b"b.S.String()b.String() // 不等价于 b.S.String()
byte alignment
struct { byte string byte}unsafe32
Compare
struct { string byte byte}unsafe24