This is a creation in Article, where the information may have evolved or changed.
Brief introduction
In the go-structure to the struct do a simple introduction, this article refer to the following bibliography continue to enrich the topic.
- "Web Development with Go" Chapter 3
- "The Go programming Language" 4.4 Page99
Pointer and Nonpointer method receiver
In the sample code given in the Go-structure article, the pointer receiver is used. In fact, Nonpointer receiver can also be used. In fact, you can also use Nonpointer
Example:
Package main import ("FMT") type-point struct {x, y int}func (P-point) GetX () int {return P.x}func (P-point) G Ety () int {return P.y}func (P *point) SetX (ix int) {(*p). x = Ix}func (P *point) sety (iy int) {(*p). y = Iy}func (P *point) SetY2 (iy int) {p.y = Iy}func (P point) SetY3 (iy int) {p.y = Iy}func test1 () {p: = Point{1, 2} fmt. Print (P, "\ T") fmt. Print ("x=", P.getx (), "\ T") fmt. Print ("y=", P.gety (), "\ T") P.setx (3) p.sety (4) fmt. Print (P, "\ T") (&P). SetX (5) (&p). Sety (6) fmt. Print (P, "\ T") fmt. Println ()}//sety () SetY2 () func test2 () {p: = Point{1, 2} fmt. Print (P, "\ T") fmt. Print ("x=", P.getx (), "\ T") fmt. Print ("y=", P.gety (), "\ T") P.setx (3) P.sety2 (4) fmt. Print (P, "\ T") (&P). SetX (5) (&p). SetY2 (6) fmt. Print (P, "\ T") fmt. Println ()}//sety () SetY3 () func test3 () {p: = Point{1, 2} fmt. Print (P, "\ T") fmt. Print ("x=", P.getx (), "\ T") fmt. Print ("y=", p.gety(), "\ T") P.setx (3) p.sety3 (4) fmt. Print (P, "\ T") (&P). SetX (5) (&p). SetY3 (6) fmt. Print (P, "\ T") fmt. Println ()}func test4 () {p: = &point{1, 2} fmt. Print (P, "\ T") fmt. Print ("x=", P.getx (), "\ T") fmt. Print ("y=", P.gety (), "\ T") P.setx (3) p.sety (4) fmt. Print (P, "\ T")//calling method SetX with receiver &p (type **point) requires explicit dereference//(&P). SetX (5)//(&P). Sety (6)//fmt. Print (P, "\ T") fmt. Println ()}/*d:\examples>go run Helloworld.gotest1: {1 2} x=1 y=2 {3 4} {5 6}test2: {1 2} x=1 y=2 {3 4} {5 6}test3: {1 2} x=1 y=2 {3 2} {5 2}test4: &{1 2} x=1 y=2 &{3 4}d:\examples>*/func Main () {FMT. Print ("test1:") test1 () fmt. Print ("test2:") test2 () fmt. Print ("test3:") test3 () fmt. Print ("test4:") test4 ()}
Points
Copied from: "Web development with Go" Chapter 3, Page 39
If you want to modify the data of a receiver from the method, the receiver must is a pointer
If the struct has a pointer receiver on some its methods, it's better to use it for the rest of the methods because it en Ables better consistency and predictability for the struct behaviors.
Pointer & dot notation
The special meanings of pointer types and. Operators are described here. In the previous example, the following code is available:
func (p *Point) SetY2(iy int) { p.y = iy}
It is equivalent to:
func (p *Point) SetY2(iy int) { (*p).y = iy}
This syntax is a very special point in go. The evidence for this grammar stems from the "Go programming Language" 4.4 Page 100, the code is as follows:
type Employee struct { ID int Name string Address string DoB time.Time Position string Salary int ManagerID int}
The dot notation also works with a pointer to a struct:
var employeeOfTheMonth *Employee = &dilbertemployeeOfTheMonth.Position += " (proactive team player)"
The last statement was equivalent to
(*employeeOfTheMonth).Position += " (proactive team player)"
This syntax of the go pointer simplifies the code.
Structure Self-nesting
A struct must not have an S member inside it, but can have a member pointing to S (Pointer *s).
type tree struct { value int left, right *tree}
Comparing struct objects
The = = operator cannot be overloaded in go, and for two objects to compare (the same) struct, each field is required to be comparable. And in comparison, we compare each field in turn. You can also customize the function of the comparison, such as equals ().
Package Main import ("FMT") type-point struct {x, y int}func (P-point) GetX () int {return P.x}func (P point) GetY () int {return P.y}func (P *point) SetX (ix int) {p.x = Ix}func (P *point) sety (iy int) {p.y = Iy}func (P * Point) equals (point) bool {return p.x = = Point.x}func Equals (P1 point, P2 point) bool {return p1.x = = P2.x}f UNC main () {p1: = Point{1, 2} p2: = Point{x:1, y:2} p3: = Point{1, 3} fmt. Println (P1 = = P2, p2 = = p3)//True, False fmt. Println (p1.x = = p2.x && p1.y = = p2.y)//True. Equals to "P1 = = P2" FMT. Println (P1. Equals (p3))//True FMT. Println (Equals (P2, p3))//true}