当前位置:网站首页>Go ---- variable usage in go language
Go ---- variable usage in go language
2022-07-26 22:03:00 【redrose2100】
List of articles
One 、Go Introduction to variables in language
Variables are abstract concepts in computer language that can store calculation results or represent values . Different variables may hold different data types .
Two 、Go How to use variables in language
2.1 Declare variables
Go Variables in a language need to be declared before they can be used , Duplicate declarations are not supported within the same scope . also Go Language variables must be declared with .
Syntax for declaring variables
var identifier type
among :
var: Declare variable keywords
identifier: Variable name
type: Variable type
for example
package main
import "fmt"
func main() {
var name string
var age int
var ok bool
}
2.2 Batch statement
Use one var keyword , Write some variables in parentheses () in
package main
func main() {
var (
name string
age int
ok bool
)
}
2.3 Initialization of a variable
Go When languages declare variables , It will automatically initialize the memory area corresponding to the variable . Each variable is initialized to the default value of its type , for example : The default values for integer and floating point variables are 0. The default value of string variable is empty string “”. Boolean variables default to false. section 、 function 、 The default value of pointer variable is nil.
Variable initialization syntax
var Variable name type = expression
for example
package main
func main() {
var name string = " Lao Guo "
var site string = "www.duoke360.com"
var age int = 30
}
2.4 Type derivation
When we declare variables , Type derivation can be carried out according to the initialization value , Thus omitting the type .
package main
func main() {
var name = " Lao Guo "
var site = "www.duoke360.com"
var age = 30
}
2.5 Initialize multiple variables
You can initialize multiple variables at once , Separated by commas .
package main
func main() {
var name, site, age = " Lao Guo ", "www.duoke360.com", 30
}
2.6 Short variable declaration
Inside the function , have access to := Operator to declare and initialize variables .
package main
func main() {
name := " Lao Guo "
site := "www.duoke360.com"
age := 20
}
Be careful : This method is only suitable for inside functions , Function cannot be used outside .
2.7 Anonymous variables
If we receive multiple variables , Some variables are not used , You can use underscores _ Represents the variable name , This variable is called anonymous variable . for example :
package main
import "fmt"
func getNameAndAge() (string, int) {
return " Lao Guo ", 30
}
func main() {
name, _ := getNameAndAge()
fmt.Printf("name: %v\n", name)
}```
边栏推荐
- Pbootcms一级栏目下的二级三级栏目高亮
- 正规方程法(Normal Equation)原理以及与梯度下降法的区别
- Thoroughly understand the principle and implementation of service discovery
- 06 cp 命令
- Pytorch torch.add() torch.add_() 用法
- Vb.net chart1 processing
- 6、 Wechat applet release process
- My SQL is OK. Why is it still so slow? MySQL locking rules
- A bowl of noodles in a dream
- d和c的符区别
猜你喜欢

matlab 基音周期估计后处理

Technology sharing | do you know the functions of the server interface automated testing and requests library?

Triangular wave spectrum of MATLAB excitation model

imshow()函数后面如果不加waitKey()函数就不显示

OPPO 自研大规模知识图谱及其在数智工程中的应用

matlab 短时自相关实现

LDAP——实现用户统一登录管理

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

Altium designer 22 modify the layer properties of the selected component

Thoroughly understand the principle and implementation of service discovery
随机推荐
补充—非线性规划
Go----Go 语言中的标识符和关键字
Ren Zhengfei talked about the suppression of the United States again: to live is to win, and to defeat the United States
Pytorch 使用RNN模型构建人名分类器
JDBC summary
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
d和c的符区别
The United States once again challenged: Forbidding contractors to purchase the equipment and technology of these five Chinese companies
Technology sharing | do you know the functions of the server interface automated testing and requests library?
Join method in JS
逻辑漏洞----任意账号注册
拼多多获得搜索词推荐 API
day07-
实际权威来自信息优势
xshell7个人免费下载,使用
安全浏览器“隐身”模式可以查看历史记录吗?
A bowl of noodles in a dream
Thoroughly understand the principle and implementation of service discovery
Knowledge base tools | wechat, document center, image display page can be generated by dragging (with template, directly used)
Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated