当前位置:网站首页>go语言的变量声明
go语言的变量声明
2022-06-23 13:35:00 【梦飞】
文章目录
1 一般语法
var 变量名 变量类型多个同类型变量可以声明成一条语句:
var 变量名1, 变量名2, 变量名3 变量类型变量声明可以是包级的或者函数级的,如:
package main
import "fmt"
var c, python, java bool // 包级变量声明
func main() {
var i int // 函数级变量声明
fmt.Println(i, c, python, java)
}2 添加初始化器
可以给每个变量添加一个初始化器(initializer):
var 变量名1, 变量名2 变量类型 = 初始化器1, 初始化器2注意初始化器的个数必须与变量个数相同。 有初始化器时,变量类型可以省略,该变量的类型会根据初始化器自动推断。 例子:
package main
import "fmt"
var i, j int = 1, 2
func main() {
var c, python, java = true, false, "no!" // 有初始化器时,变量类型可以省略
fmt.Println(i, j, c, python, java)
}3 简化变量声明
在函数内,当采用隐式类型声明时(有初始化器,省略变量类型),采用采用更简化的语句:
变量名 := 初始化器例子:
package main
import "fmt"
func main() {
var i, j int = 1, 2
k := 3
c, python, java := true, false, "no!"
fmt.Println(i, j, k, c, python, java)
}注意:在函数外,每个语句都必须以关键字(var, func等)开始,所以不能使用 := 的结构。
4 成块地声明变量
变量可以声明成一个块中,使得结构更加明了:
package main
import (
"fmt"
"math/cmplx"
)
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)
func main() {
fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
fmt.Printf("Type: %T Value: %v\n", z, z)
}5 基本类型
go的基本类型有:
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
// represents a Unicode code point
float32 float64
complex64 complex128整型数字一般使用int类型,除非有特殊的理由使用其他字节长度或无符号的整型。 没有初始化器时,各种类型的默认值:
- 数字类型:0
- bool类型:false
- string类型:""
6 类型转换
当赋值的类型与变量类型不同时,需要类型转换:
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)或者可以简化为:
i := 42
f := float64(i)
u := uint(f)注意go没有隐式类型转换,必须使用显示类型转换。
7 类型推断
当我们使用隐式类型声明时,变量类型会根据声明语句右边的值(初始化器)进行推断。
- 当右边的值为已知类型的变量,则左边变量的类型为同一类型:
var i int
j := i // j 也为 int 类型- 当右边的值为数字字面量常数时,则左边的变量为int, float64, 或 complex128 类型。
i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128- 当右边的值为bool或string字面量时,左边的变量为相应的类型。
8 常数类型
常数的声明与变量类似,只不过将var关键字改为const,并且不能使用 := 进行声明。 例子:
package main
import "fmt"
const Pi = 3.14
func main() {
const World = "world"
fmt.Println("Hello", World)
fmt.Println("Happy", Pi, "Day")
const Truth = true
fmt.Println("Go rules?", Truth)
}9 数字常数
数字常量为高精度的值。 当数字常量未声明类型时,它的类型不会像变量一样根据右边的值进行推断,而会在使用时根据上下文确定类型。 例子:
package main
import "fmt"
const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
// Shift it right again 99 places, so we end up with 1<<1, or 2.
Small = Big >> 99
)
func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
return x * 0.1
}
func main() {
fmt.Println(needInt(Small)) // 21
fmt.Println(needFloat(Small)) // 0.2
fmt.Println(needFloat(Big)) // 1.2676506002282295e+29
fmt.Println(needInt(Big)) // 报错:constant 1267650600228229401496703205376 overflows int
}边栏推荐
- Vulnhub target os-hacknos-1
- As a software testing practitioner, do you understand your development direction?
- Teach you how to build Tencent cloud server (explanation with pictures and pictures)
- Hot Recruitment! The second Tencent light · public welfare innovation challenge is waiting for you to participate
- Tinder security cooperates with Intel vPro platform to build a new pattern of software and hardware collaborative security
- 今年英语高考,CMU用重构预训练交出134高分,大幅超越GPT3
- The company has only one test, but the leader asked me to operate 1000 mobile numbers at the same time
- Short talk about community: how to build a game community?
- Deci 和英特尔如何在 MLPerf 上实现高达 16.8 倍的吞吐量提升和 +1.74% 的准确性提升
- Flutter Clip剪裁组件
猜你喜欢

From the establishment to the actual combat of the robotframework framework, I won't just read this learning note

MySQL installation

首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
![[Course preview] AI meter industry solution based on propeller and openvino | industrial meter reading and character detection](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Course preview] AI meter industry solution based on propeller and openvino | industrial meter reading and character detection
![[in depth understanding of tcapulusdb technology] tcapulusdb construction data](/img/0c/33d9387cd7733a0c6706a73f9535f7.png)
[in depth understanding of tcapulusdb technology] tcapulusdb construction data

Acquisition of wechat applet JSON for PHP background database transformation

Unity realizes the function of playing Ogg format video
![[deeply understand tcapulusdb technology] one click installation of tmonitor background](/img/0a/742503e96a9b51735f5fd3f598b9af.png)
[deeply understand tcapulusdb technology] one click installation of tmonitor background

KS003基于JSP和Servlet实现的商城系统

MIT 6.031 reading5: version control learning experience
随机推荐
Instructions for laravel8 Beanstalk
[untitled]
When pandas met SQL, a powerful tool library was born
How to install the DTS component of SQL server2008r2 on win10 64 bit systems?
Former amd chip architect roast said that the cancellation of K12 processor project was because amd counseled!
2022 soft science university professional ranking released! Xi'an electric AI ranked higher than Qingbei, and Nantah ranked first in the country!
[deeply understand tcapulusdb technology] tmonitor module architecture
leetcode:42. Rain water connection
Ks007 realizes personal blog system based on JSP
Vulnhub target os-hacknos-1
【深入理解TcaplusDB技术】单据受理之表管理
[deeply understand tcapulusdb technology] tmonitor background one click installation
Tinder security cooperates with Intel vPro platform to build a new pattern of software and hardware collaborative security
智能数字看板解决方案
面向 PyTorch* 的英特尔 扩展助力加速 PyTorch
Pyqt5 designer making tables
Multi-Camera Detection of Social Distancing Reference Implementation
SQLserver2008r2安装dts组件不成功
Oracle进入sqlplus 报错
【深入理解TcaplusDB技术】如何实现Tmonitor单机安装