Go Structural body

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

Go Structural body

    • Go Structural body
      • Defined
      • Assign value
      • Factory method

Go supports user-defined types in the form of structs.

A struct is a composite type (composite types) that, when it is necessary to define a type that consists of a series of attributes, each with its own type and value, should use the struct, which aggregates the data together. The data can then be accessed as if it were part of a separate entity.

Defined

The general way to define a struct is as follows:

typestruct {    field1 type1    field2 type2    ...}

Assign value

Structs are value types and can therefore be created by using new.

We can use . to assign values to a field:

structname.fieldname = value

Similarly, you can use . the Get struct field value:

structname.fieldname

This is called selector in the Go language. Regardless of whether the variable is one 结构体类型 or the other 结构体类型指针 , use the same selector-notation to refer to the field of the struct:

typestructint }var v myStruct   // v 是结构体类型变量var p *myStruct  // p 是指向一个结构体类型变量的指针v.ip.i

Let's look at an example

//rectangle.go  package  recttype  Rectangle struct  {length int  width int }func  (R *rectangle) Set (x, y int ) {r.length = x r.width = Y}func  (R *rectangle) area () (Res int ) {res = R.length * r. Width return }func  (R *rectangle) Perimeter () ( Res int ) {res = (r.length + r.width) * 2  return }  
// main.gopackage mainimport (    "rect"    "fmt")func main() {    new(rect.Rectangle)    rectangle.Set(1, 2)    fmt.Println("rectangle is", rectangle)    fmt.Println(rectangle.Area())    fmt.Println(rectangle.Perimeter())}

Run results

is &{12}26

Note : If XX is a struct type, then new(XX) the expression and &XX{} is equivalent.

For example:

typestruct {    int    intnew(MyStruct)my.x = 1my.y = 2等价于my2 := &Mystrcut{1, 2}

Factory method

You suddenly think, can't let others see my structure, I should make it private, but also want to let others use my structure, what should I do?

For convenience, a factory is typically defined for a type, and by convention, the name of the factory begins with new or new, which returns a pointer to the struct instance. The form is as follows:

// mystruct.gopackage mytypestruct {    ...}func NewMystruct() *mystruct {    new(mystruct)  // 初始化 m    return m}

Use the factory method in other packages:

// main.gopackage mainimport"my"new(my.mystruct)  // 编译失败,mystruct 是私有的right : = my.NewMystruct()  // 实例化 mystruct 的唯一方式
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.