This is a creation in
Article, where the information may have evolved or changed. This is the 3rd tutorial of our [Golang Series Tutorial] (/SUBJECT/2), which explores variables (Variables) in Golang. You can read the Golang series [* * Tutorial part 2nd: Hello world**] (/articles/11755), learn how to configure Golang, and run the Hello World program. The # # # variable is a variable that specifies the name of a storage unit (memory location) that stores values of a specific type. In Go, there are several syntax for declaring variables. # # # Declares a single variable **var name type** is the syntax for declaring a single variable. "' Gopackage mainimport" FMT "Func Main () {var age int//variable declaration FMT. Println ("My Age was", age)} ' [Online Run Program] (Https://play.golang.org/p/XrveIxw_YI) statement ' var age int ' Declares a variable of type int, named age. We have not yet assigned a value to the variable. If the variable is not assigned a value, Go automatically initializes it, assigning a value of 0 (Zero value) to the variable type. In this case, age is assigned a value of 0. If you run the program, you will see the following output: ' My age is 0 ' variable can be assigned to any value of this type. Age in the previous program can be assigned a value of any integer value. "' Gopackage mainimport" FMT "Func Main () {var age int//variable declaration FMT. Println ("My Age was", age) Age = 29//Assignment FMT. Println ("My Age was", age) Age = 54//Assignment FMT. Println ("My New Age was", age)} "[Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/Z4NKMJBXLX) above the program will have the following output:" ' My age is 0 my age is my New Age is 54 "# # # declares the variable and initializes the declaration variable at the same time as the initial value can be given. **var name type = initialvalue** syntax is used toDeclares a variable and initializes it. "' Gopackage mainimport" FMT "Func Main () {var age int = 29//Declare variable and initialize FMT. Println ("My Age was", age)} "[Running Program Online] (HTTPS://PLAY.GOLANG.ORG/P/TFFPZSRCHH) in the above program, age is an int type variable with an initial value of 29. If you run the above program, you can see the following output, confirming that age has been initialized to 29. ' My age is 29 ' ' # # # type inference (type inference) if the variable has an initial value, Go can automatically infer the type of the variable with the initial value. Therefore, if the variable has an initial value, you can omit the ' type ' in the variable declaration. If the syntax for a variable declaration is **var name = Initialvalue**,go can automatically infer the type of the variable based on the initial value. In the following example, you can see that in line 6th, we omit the ' int ' type of the variable ' age ', and Go still infers that it is of type int. "' Gopackage mainimport" FMT "Func Main () {var age = 29//Can infer type FMT. Println ("My Age was", age)} "[Running Program Online] (https://play.golang.org/p/FgNbfL3WIt) # # # Declares multiple variables go can declare multiple variables through a single statement. The syntax for declaring multiple variables is **var name1, name2 type = initialvalue1, initialvalue2**. "' Gopackage mainimport" FMT "Func Main () {var width, height int = 100, 50//Declare multiple variables FMT. Println ("width is", width, "height is", heigh)} "[Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/4AOQYT55AH) The above program will print ' width ' in standard output Is the height is 50 '. You might have thought that if width and height were omitted from initialization, their initial values would be assigned a value of 0. "' Gopackage mainimport"Fmt "Func main () {var width, height int fmt. Println ("width is", width, "height is", height) width = height = + FMT. Println ("New width is", width, "new height is", height)} "[Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/DM00PCBBSU) above the program will print:" ' width is 0 height are 0 new width is the new height is 50 ' ' In some cases, we might want to declare variables of different types in a statement. The syntax is as follows: "' Govar (name1 = initialvalue1, name2 = initialvalue2) ' Using the above syntax, the following program declares different types of variables. "Gopackage mainimport" FMT "Func Main () {var (name =" Naveen "age = height int) fmt. Println ("My Name is", "name", "," "," "," "," "," "," "," "," "", "" "," "" "(Https://play.golang.org/p/7pkp74h_9L) here We have declared the name of the **string type, age and height** of type int (we will discuss the types of variables supported by Golang in the next tutorial). Running the above program produces the output ' My name is Naveen, which is 0 '. # # # Short declaration go also supports a concise form of declaring variables, called short Hand Declaration, which uses the **:=** operator. The short syntax for declaring variables is **name: = initialvalue**. "' Gopackage mainimport" FMT "Func Main () {name, Age: =" Naveen ", 29//Short declaration FMT. Println ("My name is", Name, "Age was", age)} "[Running Program Online] (HTTPS://PLAY.GOLANG.ORG/P/CTQGW4W6KX) runs the above program, you can see the output as ' My name is Naveen 29 '. A short declaration requires that all variables on the left side of the **:=** operator have an initial value. The following program will throw an error ' cannot assign 1 values to 2 variables ' because **age is not assigned * *. "Gopackage mainimport" FMT "Func Main () {name, Age: =" Naveen "//error fmt. The syntax requirements for PRINTLN ("My name is", Name, "Age was", age)} ' [Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/WZD2HMDVQW) Short declaration are required **:=** At least one variable on the left side of the operator is not yet declared. Consider the following program: "' Gopackage mainimport" FMT "Func Main () {A, B: = 20, 30//Declare variables A and B fmt. Println ("A is", A, "B is", b) b, c: = +, +//b has been declared, but C has not yet declared FMT. Println ("B is", B, "C is", C) b, C = 80, 90//assigns a new value to the already declared variable B and C fmt. Println ("changed B is", B, "C is", C)} "[Online Run Program] (Https://play.golang.org/p/MSUYR8vazB) in the above program in line 8th, since B has been declared, and C has not yet been declared, so The operation succeeds and outputs: "A is a-B is a-B is a-C is a-changed B is a-C is 90" "but if we run the following program:" ' Gopackage mainimport "FMT" func main () {A, B: = 20, 30//Declare A and B FMT. Println ("A is", A, "B is", b) A, B: = 40, 50//error, no undeclared variable} ' [Online Run program] (HTTPS://PLAY.GOLANG.ORG/P/EYTTRNLDU3The above run will throw a ' no new variables on the left side of: = ' ERROR, because the variables of a and B have been declared, and there are no undeclared variables on the of **:=**. Variables can also be assigned at run time. Consider the following program: "' Gopackage mainimport (" FMT "" math ") func main () {A, B: = 145.8, 543.8 c: = Math. Min (A, b) fmt. PRINTLN ("Minimum value is", c)} ' [Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/7XOJATRPH9) in the above program, the value of C is calculated during the run process, that is, a and b minimum values. The above program will print: "' Minimum value is 145.8" because Go is a strongly typed (strongly Typed) language, so a variable of one type is not allowed to be assigned a value of another type. The following program throws an error ' cannot use ' Naveen (type string) as type int in assignment ', which is because the age was declared to be of type int, and we tried to assign it a value of type string. "' Gopackage MainFunc Main () {age: = +//age is an int type age =" Naveen "///error, try to assign a string to the INT type variable}" "[Run Program Online] (Https://play.gola NG.ORG/P/K5RZ4GXJPJ) Thank you for your reading, please post your questions and feedback in the comments section. * * Previous tutorial-[Hello World] (https://studygolang.com/articles/11755) * * * * Next tutorial-[type] (https://studygolang.com/articles/ 11869) * *
via:https://golangbot.com/variables/
Author: Nick Coghlan Translator: Noluye proofreading: Unknwon
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
5,422 reads ∙1 likes