Iv. Golang Type Explanation--go language study notes

Source: Internet
Author: User
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

    1. A "key-value" pair
    2. Key points: attention to concurrent read and write situation, locked
    3. Cannot write to uninitialized map, throws exception (panic)
    4. 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

    1. Go has pointers, but there is no pointer operation. You cannot traverse the individual bytes of a string with a pointer variable.
    2. The Go pointer is just a reference.
    3. 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

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.