* * This is the third Golang language Learning Tutorial **var declaration statement You can create a variable of a specific type, then append a name to the variable and set the initial value of the variable. The variable declaration syntax is as follows ***:>**var variable name type = Expression * * Example: "' govar age int ="-----------------where * * "type" * * or "= expression" * * Two can omit one of them. > If Type * * is omitted, type information is determined based on the initial expression. If you omit the **= expression * *, the variable is initialized with a value of 0. Example 1: "Gopackage mainimport" FMT "Func Main () {var age = 19//omit type, based on an automatic expression to determine the age type FMT. Println ("Hello world,my is", age)} "the program above will print: >hello world,my is 19 example 2: ' Gopackage mainimport ' FMT ' func main ( ) {var age int//omit expression, which is initialized with 0 by default, FMT. Println ("Hello world,my is", age)} "the program above will print: >hello world,my is 0# declares multiple variables go can declare multiple variables with a single statement. Declare multiple variable syntax as follows ***:>var name1, name2 type = initialvalue1, initialvalue2 example: ' Gopackage mainimport ' FMT ' func main () {var Height,weight = 178,57//declaration of two variables height, weight fmt. Println ("My height is", the height, "and my weight is", weight)} "' Above the program will print: >my height is 178 and my weight is 57 in some cases we may need to Declare variables of different types in one statement. A statement declares different types of variable syntax as follows: "' Govar (name1 = initialvalue1 name2 = initialvalue2) Example: ' Gopackage mainimport ' FMT ' func Main () {var (name = "Xunk" age = height = 178) fmt. Println ("My Name is", "name", ",", "", "", "", "", "", "", "", "" and "Heiget", "height")} ' will print: >my name is Xunk Y Heiget is 178# Short declaration go supports a concise way of declaring variables, using the: = operator, called a short declaration. The short declaration syntax is as follows: ***>name: = InitialValue Example: "Gopackage mainimport" FMT "Func Main () {name,age: =" Xunk ", 19//Short declaration Name,age Fmt. Println ("My Name is", "name", "My Age is", and "age") above the program will print: >my name is Xunk my age is 19:= short declaration all the variables on the left have an initial value, and the operator must have a variable The amount is not yet declared. Variables can also be assigned at run time. Example: ' Gopackage mainimport ("FMT" "math") func main () {b: = 25.3,73.1//declares a B variable c: = Math. Min (A, B)//compare A, B minimum value, assign to C FMT. Println ("The minimum values is", c)} "the program above will print: >the minimum values is 25.3\ because Go is a strongly typed (strongly Typed) language, Therefore, a variable of one type is not allowed to be assigned a value of another type. "' ' Gopackage mainfunc Main () {age: = +///is an int type =" Naveen "///error, trying to assign a string to an int type variable}" * Above program will error:*> (type Strin g) as type int in assignment it is not possible to assign a string to an int type variable 81 reads
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.