当前位置:网站首页>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)
}```
边栏推荐
- It is said that Samsung obtained EUV photoresist from Belgium
- 仅需一个依赖给Swagger换上新皮肤,既简单又炫酷~
- Leetcode exercise - Sword finger offer II 005. maximum product of word length
- Tester: "I have five years of testing experience" HR: "no, you just used one year of work experience for five years."
- Basic operation of (C language) files
- 月薪5万的朋友告诉我,你只是在打杂
- 浏览器主页被篡改怎么办,主页被篡改恢复方法
- In depth interpretation of happens before principle
- Also on Data Governance
- Flask对token的解码&挂载&装饰器&七牛云上传
猜你喜欢

Use of cmake

Flink's real-time data analysis practice in iFLYTEK AI marketing business

Thorough load balancing

A new technical director asked me to do an IP territorial function~

Jd.com: how does redis realize inventory deduction? How to prevent goods from being oversold?

内容管理工具,用蓝色书签就足够

09 expr 命令

Xshell7 personal free download, use

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

新来个技术总监要我做一个 IP 属地功能~
随机推荐
Is it safe to open an account on flush? How to choose a securities firm for opening an account
Attack and defense world ----- ics-07
Flink's real-time data analysis practice in iFLYTEK AI marketing business
Basic operation of (C language) files
Jd.com: how does redis realize inventory deduction? How to prevent goods from being oversold?
我的sql没问题为什么还是这么慢|MySQL加锁规则
Content management tools, blue bookmarks are enough
Xshell7 personal free download, use
The United States once again challenged: Forbidding contractors to purchase the equipment and technology of these five Chinese companies
day07-
织梦提示DedeCMS Error:Tag disabled:php!
If you do not add waitkey() function after imshow() function, it will not be displayed
JDBC summary
Flash source code startup phase
京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
Pytorch squeeze() unsqueeze() 用法
Supplement - nonlinear programming
matlab 激励模型 三角波频谱
matlab 短时自相关实现
Thoroughly understand the principle and implementation of service discovery