Golang Learning Notes-1.3 variables

Source: Internet
Author: User

This article is the third Golang language learning tutorial
The Var declaration statement 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

Cases:

var age int = 19

where "type" or "= expression" Two can omit one of the two.

If the type is omitted, the type information is determined based on the initial expression.
If the = Expression is omitted, the variable is initialized with a value of 0.

Example 1:

package mainimport "fmt"func main() {        var age = 19 //省略类型,根据自动表达式确定age类型        fmt.Println("Hello World,my age is",age)}

The above program will print:

Hello World,my Age is 19

Example 2:

package mainimport "fmt"func main(){    var age int //省略表达式,默认用0初始化该变量    fmt.Println("Hello World,my age is",age)}

The above program will print:

Hello World,my Age is 0

Declaring multiple variables

Go is able to declare multiple variables through a single statement.
The syntax for declaring multiple variables is as follows :

var name1, name2 type = initialvalue1, initialvalue2

Cases:

package mainimport "fmt"func main(){        var height,weight = 178,57 //声明两个变量身高,体重        fmt.Println("my height is",height,"and my weight is",weight)}

The above 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 :

var (   name1 = initialvalue1   name2 = initialvalue2)例:```gopackage mainimport "fmt"func main(){       var (           name = "xunk"           age = 19           height = 178       )       fmt.Println("my name is",name,",my age is",age,"and my heiget is",height)}

The above program will print:

My name is Xunk, my age was and my heiget is 178

Brief statement

Go supports a concise way of declaring variables, using the: = operator, called a short declaration.
The short declaration syntax is as follows:

Name: = InitialValue

Cases:

package mainimport "fmt"func main(){        name,age := "xunk",19 //简短声明name,age        fmt.Println("my name is",name,"my age is",age)}

The above program will print:

My name is Xunk my 19

: = Short declaration All variables on the left have an initial value, and the left side of the operator must have a variable that is not yet declared.

Variables can also be assigned at run time.
Cases:

package mainimport (        "fmt"        "math")func main(){        a,b := 25.3,73.1 //声明a,b变量        c := math.Min(a,b) //比较a,b最小值,赋值给c        fmt.Println("The minimum values is",c)}

The above program will print:

The minimum values is 25.3


Because Go is a strongly typed (strongly Typed) language, a variable of one type is not allowed to be assigned a value of another type.

package mainfunc main() {      age := 29      // age是int类型    age = "naveen" // 错误,尝试赋值一个字符串给int类型变量}

The above procedure will be error:

(type string) as type int in assignment

You cannot assign a string to an int type variable

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.