In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly talks about "what is the structure of Go basic programming". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the structure of Go basic programming"?
A struct is a custom way to form a new data type, and a structure is a compound type with members in the type. An Go language structure is an aggregated data type, which is an entity composed of zero or more values of any type. Each value is called a member of the structure. To describe the various attributes of real-world entities and entities.
Structure properties are also called fields or members, and each field has a name and type, and each name is unique. It can be any type, such as normal type, compound type, function, map, interface, struct, etc., so we can understand it as a "class" in the go language.
Define
The structure is defined as follows:
Type name struct {fieldName1 type1 fieldName2 type2...}
Define the User structure as follows:
Type User struct {Name string age int}
Instantiation
The above definition is just a type, just like an int, you need to define a type variable before you can use a class similar to Java.
Define variable usage directly
Package main import ("fmt") type User struct {Name string age int} func main () {var user1 User / / define User type variable user var user2 * User / / type pointer, no memory allocated, fmt.Println (user1, user2) / / {0}} cannot be used directly
Define default member variables
Var user1 = User {Name: "abc"} fmt.Println (user1) func NewUser () * User {return & User {Name: "abc", age:20}}
Use the built-in function new () to allocate memory and return type variable pointers
Var user = new (User) fmt.Println (user) / / & {0}
Access to members
Use. To visit
Var user User user.Name = "abc" user.age = 20 fmt.Println (user) / / {abc 20}
The capitalization of the first letter indicates that the member is visible outside the package (that is, the object-oriented public property), but not outside the lowercase package.
Zero value: the zero value of the structure is nil
Initial value: when the initial value of the structure is non-nil, the initial value of the corresponding type of each member
Empty structure: an empty structure is a structure without fields, and an empty structure does not occupy memory
Package main import ("fmt"unsafe") func main () {user1: = struct {} {} user2: = struct {} fmt.Printf ("% pforce% DN", & user1, unsafe.Sizeof (user1)) / / 0x585218user1 0 fmt.Printf ("% pforce% DN", & user2, unsafe.Sizeof (user2)) / / 0x585218Cool 0}
From the above, we can see that the address and size of the empty structure are the same. According to this characteristic, the use of empty structure can be used as a semaphore, which acts as a signal but does not occupy memory. For example, chan of empty structure type
Anonymous structure
Anonymous structures do not have a type name and can be used directly without being defined by the type keyword.
User: = struct {Name string} {Name: "abc"} fmt.Println (user) / / {abc}
Compare
If all members of a structure are comparable, and the order, type, and number of members are exactly the same, the two structures can be compared using the = = or! = operator.
Package main import ("fmt") func main () {user1: = struct {Name string} {Name: "abc"} user2: = struct {Name string} {Name: "abc"} fmt.Println (user1 = = user2) / / true}
Member names are not the same
Package main import ("fmt") func main () {user1: = struct {Name: "abc"} user2: = struct {name string} {name: "abc"} fmt.Println (user1 = = user2) / / invalid operation: user1 = = user2 (mismatched types struct {Name string} and struct {name string})}
The number of members is different.
Package main import ("fmt") func main () {user1: = struct {Name: "abc"} user2: = struct {Name string age int} {Name: "abc"} fmt.Println (user1 = = user2) / / invalid operation: user1 = = user2 (mismatched types struct {Name string} and struct {Name string; age int})}
Member types cannot be compared
Package main import ("fmt") func main () {user1: = struct {Name string m mapping int} {Name: "abc"} user2: = struct {Name string m mapping [abc] int} {Name: "abc"} fmt.Println (user1 = = user2) / / invalid operation: user1 = = user2 (struct containing mapping [int cannot be compared])}
The order is different.
Package main import ("fmt") func main () {user1: = struct {Name: "abc"} user2: = struct {age int Name string} {Name: "abc"} fmt.Println (user1 = = user2) / / invalid operation: user1 = = user2 (mismatched types struct {Name string; age int} and struct {age int; Name string})}
In fact, the whole structure is a type (such as int). If the member order and type are different, the overall structure is different, so it cannot be compared for strongly typed languages, and the corresponding types are exactly the same. We also need to pay attention to whether the members are comparable, such as slice, map and so on.
The Go language does not have the concept of object-oriented, but you can think of a structure as a class that can implement object-oriented features, such as inheritance through composition and embedding.
Anonymous field
Anonymous fields are names that are not displayed by the structure, and are embedded in one or more structures, such as the following
B is directly embedded in A, and B is an anonymous field.
Package main import ("fmt") type A struct {Name string B} type B struct {Age int Name string}
Access member variables
Func main () {var a = A {Name: "a", Age:20 B {Name: "b", Age:20}} fmt.Printf ("% # vn", a) / / main.A {Name: "", B:main.B {Age:0} fmt.Println (a.Name) / / a fmt.Println (a.B.Name) / / b fmt.Println (a.Age) / / 20}
When there is only one member name, the Go syntax sugar can omit the embedded structure.
Fmt.Println (a.B.Age) / / 20 fmt.Println (a.Age) / / 20
Multiple members with the same name cannot be omitted because the compiler does not know which one
Type C struct {A B} func main () {var c = C {Name A: "a"}, Name B {Name: "b", Age:20}} fmt.Println (c.Name) / / ambiguous selector c.Name}
The right thing to do is
Func main () {var c = C {A Name: "a"}, B {Name: "b", Age:20}} fmt.Println (c.A.Name) / / a fmt.Println (c.B.Name) / / b}
Combination
The above is an embedded structure without a name, and you can also name the embedded structure. Access must be accompanied by specific fields and cannot be omitted.
Package main import ("fmt") type A struct {Btype B} type B struct {Age int Name string} func main () {var a = A {Btype:B {Name: "b", Age:20}} fmt.Println (a.Name) / / .Name undefined (type A has no field or method Name)}
Label
For example, the tag wrapped in ``at the end of the field is mainly serialized and deserialized through reflection, as described by the reflection section.
Type User struct {Id int `json: "id" `Account string `json: "account" form: "account" `Nickname string `gorm: "nickname" json: "nickname" form: "nickname" `}
Method
Methods are generally a feature of object-oriented programming (OOP). Methods in Go language are actually special functions associated with a value or variable. This value or variable is called the recipient.
Func ([typeName] recipient) name (param) [return] {}
Recipient is a custom type
Package main import ("fmt") type A struct {} / / structure type B int / / int func (an A) show () {fmt.Println ("a.")} func (b B) show () {fmt.Println ("b.")} func main () {var an A var b B A.show () b.show ()}
The recipient cannot directly use the built-in type
Func (c int) show () {/ / cannot define new methods on non-local type int fmt.Println ("b.")}
The recipient value can be a value type or a pointer type
Package main import ("fmt") type A struct {} type B struct {} func (an A) show () {/ / value type fmt.Println ("a.")} func (b * B) show () {/ / pointer type fmt.Println ("b.")} func main () { Var an A var b B a.show () b.show ()}
For B, the following two invocation methods are equivalent. In essence, they are all the same. The writing of b.show () is omitted (& b), but is completed by syntax sugar.
Func main () {var b B b.show () (& b) .show ()}
Method can access the recipient's own information, as follows
Package main import ("fmt") type User struct {Id int Account string Nickname string} func (u User) show () {fmt.Println (u.Nickname)} func main () {var a = User {Nickname: "Test"} a.show () / / Test}
The receiver of the value type copies all of the type, and the modification will not affect the original data; the pointer copies the address, and the modification will affect the original data
Package main import ("fmt") type User struct {Id int Account string Nickname string} func (u User) show () {fmt.Println (u)} func (u User) setName1 () {u.Nicknamee = "value type"} func (u * User) setName2 () {u.Nicknamee = "pointer type"} func main () {var a = User {Nickname: "Test Try "} a.setName1 () a.show () a.setName2 () a.show ()}
The recipient type itself cannot be a pointer
Package main import ("fmt") type An int type B * int / / variable type is pointer func (an A) show () {fmt.Println ("a.")} func (b B) show () {/ / invalid receiver type B (B is a pointer type) fmt.Println ("b.")} so far I believe that you have a deeper understanding of what is the structure of Go basic programming, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.