Golang variables

1. What is a variable?

A program is a world.
Variables are the basic unit of the program.

Introduction to two variables

1 concept

A quantity is equivalent to the representation of a data storage space in memory. You can think of a variable as the house number of a room. We can find the room through the house number. In the same way, the variable (value) can be accessed through the variable name.

2 Basic steps for using variables

  • Declare variables (define variables)
  • Assignment
  • use variables

3. Declaration, initialization and assignment of variables

declare variables

Basic syntax: var variable name data type
var a int This declares a variable, the variable name is a
var num1 float32 This also declares a variable, representing a single-precision type decimal, the variable name is num1

Initialize variables

When declaring a variable, give the value
var a int = 45. These are
the details of initializing variable a: If you assign the value directly when declaring, you can omit the data type
var b = 400

Assign a value to a variable

For example, you declare a variable first: var num int //default is 0
and then give the value num = 780; this is assigning a value to the variable

4 cases

package main

import "fmt"

func main() {
	//定义变量/声明变量
	var i  int
	//给i 赋值
	i = 10
	//使用变量
	fmt.Println("i=", i)

}

Will enter i= 10

5 Things to note when using variables

  • 1) A variable represents a storage area in memory
  • 2) This area has its own name (variable name) and type (data type)
  • 3) Three ways to use Golang variables.
    The first one: Specify the variable type. If no value is assigned after declaration, use the default value. The
    second one: Determine the variable type by yourself based on the value (type derivation)
    . The third one: Omit var, note: = The variable on the left should not have been declared, otherwise it will cause compilation errors
  • 4) Multi-variable declaration
    In programming, sometimes we need to declare multiple variables at once. Golang also provides such syntax
  • 5) How to declare multiple global variables at once
  • 6) The data values ​​in this area can continuously change within the same type range
  • 7) Variables cannot have the same name in the same scope (in a function or in a code block)
  • 8) Variable = variable name + value + data type. Please pay attention to this. The three elements of variables
  • 9) If a Golang variable is not assigned an initial value, the compiler will use a default value, such as int with a default value of 0, string with a default value of empty string, and decimal with a default value of 0.

1)-2)

image.png

3)-4)

package main

import "fmt"

func main() {
    
    
	//golang 的变量使用方式1
	//第一种:指定变量类型,声明后若不赋值,使用默认值
	// int的默认值是0,其他数据类型的默认值后面马上介绍
	var i  int
	// , 表示输出完前面的,接着输出后面的
	fmt.Println("i=", i)
	
	
	//第二种:根据值自行判定变量类型(类型推导)
    var num = 10.11
	fmt.Println("num=", num)
	
	//第三种:省略var,注意:=左侧的变量不应该是已经声明过的,否则会导致编译错误
	//下面的方式等价 
	//var name string  
    //name = "tom"
    // := 的 :不能省略,否则错误
	name := "tom"
    fmt.Println("name=", name)
    
    //一次性声明多个变量的方式1
	var n1, n2, n3 int
    fmt.Println("n1=",n1,"n2=",n2,"n3=",n3)

	//一次性声明多个变量的方式2
	var a1, name2, a3 = 100, "tom", 888
	fmt.Println("a1=",a1,"name2=",name2,"a3=",a3)

	//一次性声明多个变量的方式3,同样可以使用类型推导
	s1, name3, s3 := 100, "tom~", 888
	fmt.Println("s1=",s1,"name3=",name3,"s3=",s3)

}

> go run main.go
i= 0
num= 10.11
name= tom
n1= 0 n2= 0 n3= 0
a1= 100 name2= tom a3= 888
s1= 100 name3= tom~ s3= 888

5) Global variables : How to declare multiple global variables at once

package main

import "fmt"

//定义全局变量
var n1 = 100
var n2 = 200
var name = "jack"
//上面的声明方式,也可以改成一次性声明
var (
	n3 = 300
	n4 = 900
	name2 = "mary" 
)


func main() {
    
    
	//输出全局变量
	fmt.Println("n1=",n1, "name=",name, "n2=",n2)
	fmt.Println("n3=",n3, "name2=",name2, "n3=",n3)

	} 

> go run main.go
n1= 100 name= jack n2= 200
n3= 300 name2= mary n4= 900

6)-7)

package main

import "fmt"

//变量使用的注意事项

func main() {
    
    
	//该区域的数据值可以在同一类型范围内不断变化,下面执行结果是i= 50
	//不能改变数据类型 
	var i int = 10
	i = 30
	i = 50 
	fmt.Println("i=",i)
	} 
	
	//变量在同一个作用域(在一个函数或者在代码块)内不能重名
	// var i  int = 27  在同一个作用域再定义i 就会报错
    // i  := 29  在同一个作用域再定义i 就会报错
	
	

The use of + sign in three programs

1) When both the left and right sides are numeric types, do the addition operation
2) When both the left and right sides are strings, do string concatenation

package main

import "fmt"

//演示golang中+的使用
func main() {
    
    
   var i = 1
   var j = 2
   var r = i + j  //做加法运算
   fmt.Println("r=",r)
	
   var str1 = "hello"
   var str2 = "world"
   var res = str1 + str2   //做拼接操作
   fmt.Println("res=", res)
   } 

> go run main.go
r= 3
res= helloworld

Guess you like

Origin blog.csdn.net/weixin_62173811/article/details/130497542