In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Go initialization variable method is what the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you read this Go initialization variable method is what the article will have a harvest, let's take a look.
The tactics of initializing value types in Go
The data types of Golang can be divided into value types and reference types. Let me first summarize the initialization methods of value types in Go (take string as an example):
Var a1 string
Fmt.Printf ("A1:% # v\ n", A1) / / A1: ""
Var a2 * string
Fmt.Printf ("a2:% # v\ n", a2) / / a2: (* string) (nil)
/ / panic: runtime error: invalid memory address or nil pointer dereference
/ / fmt.Printf ("a2:% # v\ n", * a2)
A3: = new (string) fmt.Printf ("A3:% # v\ n", A3) / / A3: (* string) (0xc42000e200)
Fmt.Printf ("A3:% # v\ n", * A3) / / A3: ""
A4: = "hello"
Fmt.Printf ("A4:% # v\ n", A4) / / A4: "hello"
A5: = string ("hello") fmt.Printf ("A5:% # v\ n", A5) / / a5: "hello"
A6: = & a5fmt.Printf ("A6:% # v\ n", A6) / / a6: (* string) (0xc42000e1e0)
/ / error report, cannot make type string
/ / a7: = make (string, 1)
/ / error report, cannot take the address of string ("hello")
/ / a8: = & string ("hello")
The comment part is the output information, and you can see that some of the results are typed as a value, and some are a pointer. I just want to make two points in this part:
Go automatically initializes the declaration variable to a value of 0. The so-called 0 value is: int is 0, string is empty, bool is false, and so on.
For variables created through new, it is a pointer, which is different from the variable declared by var, and the variable declared by var is only a nil. New (string) allocates a piece of memory for string, initialized to 0. You can understand through the error message noted above.
A way to initialize reference types in Go
This is the point I want to make. There are only three reference types in Go: map:: slice:: channel::,. Here we use slice as an example.
Var s1 [] string
Fmt.Printf ("S1:% # v\ n", S1) / / S1: [] string (nil)
S1 = append (S1, "hello") fmt.Printf ("S1:% # v\ n", S1) / / S1: [] string {"hello"}
Var S2 * [] string
Fmt.Printf ("S2:% # v\ n", S2) / / S2: (* [] string) (nil)
S3: = [] string {"a", "b", "c"} fmt.Printf ("S3:% # v\ n", S3) / / S3: [] string {"a", "b", "c"}
S4: = & [] string {} fmt.Printf ("S4:% # v\ n", S4) / / S4: & [] string {}
S5: = & s3fmt.Printf ("S5:% # v\ n", S5) / / S5: & [] string {"a", "b", "c"}
S6: = new ([] string) fmt.Printf ("S6:% # v\ n", S6) / / S6: & [] string (nil)
/ / first argument to append must be slice; have * [] string
/ / S6 = append (S6, "hello") / / this is a null reference pointer, so an error is reported
S7: = make ([] string, 0) fmt.Printf ("S7:% # v\ n", S7) / / S7: [] string {} / / is used in this way
S8: = new ([] string) * S8 = make ([] string, 0) fmt.Printf ("S8:% # v\ n", S8) / / S8: & [] string {}
Arr: = [5] string {"a", "b", "c"} S9: = arr [:] fmt.Printf ("S9:% # v\ n", S9) / / S9: [] string {"a", "b", "c", "", ""}
Here I will focus on analyzing the three initialization methods: S6, S7, and S8. Let's start with S6, using new.
New (T) allocates a piece of memory for each new type T, initializes to 0 and returns a memory address of type * T: this method returns a pointer to an address of type T with a value of 0.
In this case, the so-called value is 0, which is not a numerical value of 0, but the default value of go, which corresponds to:: slice:: is nil.
This is definitely not the way to initialize:: slice:: in Go. Why? Let me say a little bit here because:: slice:: in Go is defined as follows:
Type slice struct {
Array unsafe.Pointer
Len int
Cap int
}
If you can't initialize:: slice:: because it returns the memory address of T with new, you can't let slice use it normally. If you want him to use it normally, you have to initialize it with make for T again, just like S8. If you do this, do you think there is something wrong with it?
The initialization of slice requires initializing the values of len and cap so that array points to a pointer to an array. After these initializations are completed, slice can be used properly.
This is the end of the article on "what is the method of initializing variables in Go?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the method of initializing variables in Go". If you want to learn more knowledge, you are welcome to follow the industry information channel.
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.