当前位置:网站首页>Go----Go语言中的变量使用方法
Go----Go语言中的变量使用方法
2022-07-26 21:11:00 【redrose2100】
一、Go语言中变量简介
变量是计算机语言中能储存计算结果或能表示值的抽象概念。不同的变量保存的数据类型可能会不一样。
二、Go语言中变量的使用方法
2.1 声明变量
Go语言中的变量需要声明后才能使用,同一作用域内不支持重复声明。 并且Go语言的变量声明后必须使用。
声明变量的语法
var identifier type
其中:
var:声明变量关键字
identifier:变量名称
type:变量类型
例如
package main
import "fmt"
func main() {
var name string
var age int
var ok bool
}
2.2 批量声明
使用一个var关键字,把一些变量写在一个括号()里
package main
func main() {
var (
name string
age int
ok bool
)
}
2.3 变量的初始化
Go语言在声明变量的时候,会自动对变量对应的内存区域进行初始化操作。每个变量会被初始化成其类型的默认值,例如: 整型和浮点型变量的默认值为0。 字符串变量的默认值为空字符串“”。 布尔型变量默认为false。 切片、函数、指针变量的默认为nil。
变量初始化语法
var 变量名 类型 = 表达式
例如
package main
func main() {
var name string = "老郭"
var site string = "www.duoke360.com"
var age int = 30
}
2.4 类型推导
我们在声明变量时,可以根据初始化值进行类型推导,从而省略类型。
package main
func main() {
var name = "老郭"
var site = "www.duoke360.com"
var age = 30
}
2.5 初始化多个变量
可以一次初始化多个变量,中间用逗号分隔。
package main
func main() {
var name, site, age = "老郭", "www.duoke360.com", 30
}
2.6 短变量声明
在函数内部,可以使用 :=运算符对变量进行声明和初始化。
package main
func main() {
name := "老郭"
site := "www.duoke360.com"
age := 20
}
注意:这种方法只适合在函数内部,函数外面不能使用。
2.7 匿名变量
如果我们接收到多个变量,有一些变量使用不到,可以使用下划线_表示变量名称,这种变量叫做匿名变量。例如:
package main
import "fmt"
func getNameAndAge() (string, int) {
return "老郭", 30
}
func main() {
name, _ := getNameAndAge()
fmt.Printf("name: %v\n", name)
}```
边栏推荐
- A new technical director asked me to do an IP territorial function~
- 五、小程序报错:message:Error: 系统错误,错误码:80058,desc of scope.userLocation is empty
- Highlight the secondary and tertiary columns under the primary column of pbootcms
- ansible安装及使用
- 任正非再谈美国打压:活下去就是胜利,要战胜美国
- 小米1亿像素摄像头揭秘:1/1.3英寸COMS传感器、分辨率12032×9024
- Six instructions of Memcache based caching mechanism
- A friend with a monthly salary of 50000 told me that you were just doing chores
- Isilon 的OneFs常见操作命令(一)
- Li Kou daily question - day 43 -168. Name of Excel table column
猜你喜欢

A bowl of noodles in a dream

FreeRTOS personal notes - Events

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

Selenium automated test interview questions family bucket
![[mysql]substr usage - query the value of specific digits of a field in the table](/img/d5/68658ff15f204dc97abfe7c9e6b354.png)
[mysql]substr usage - query the value of specific digits of a field in the table

Vi和Vim文本编辑器

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

What to do if the browser home page is tampered with, and how to recover if the home page is tampered with

Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated

LDAP——实现用户统一登录管理
随机推荐
Talk about TCP time_ WAIT
商汤科技发布人脸识别一体机SensePass Pro
仅需一个依赖给Swagger换上新皮肤,既简单又炫酷~
Selenium自动化测试面试题全家桶
Method overloading and method rewriting
小米1亿像素摄像头揭秘:1/1.3英寸COMS传感器、分辨率12032×9024
Huawei released the top ten trends in 2025: 5g, robot, AI, etc
Flextronics responded to the "seizure of Huawei materials" incident: deeply regretted, looking forward to continuing cooperation!
Uncover the secrets of Xiaomi 100million pixel camera: 1/1.3 inch COMS sensor, resolution 12032 × nine thousand and twenty-four
If you do not add waitkey() function after imshow() function, it will not be displayed
拼多多获得搜索词推荐 API
Technology sharing | do you know the functions of the server interface automated testing and requests library?
2022年简历石沉大海,别投了,软件测试岗位饱和了....
Just one dependency to give swagger a new skin, which is simple and cool
七、微信小程序运行报错:Error: AppID 不合法,invalid appid
Search eBay product API by keyword
【音视频】ijkplayer播放器参数说明文档
6、 Wechat applet release process
Pytorch torch. add() torch. add_ () usage
伟创力回应“扣押华为物料”事件:深感遗憾,期待继续合作!