当前位置:网站首页>Variable declaration of go language
Variable declaration of go language
2022-06-23 15:34:00 【Mengfei】
List of articles
1 General grammar
var Variable name Variable type Multiple variables of the same type can be declared as a statement :
var Variable name 1, Variable name 2, Variable name 3 Variable type Variable declarations can be Package level Or Function level Of , Such as :
package main
import "fmt"
var c, python, java bool // Package level variable declaration
func main() {
var i int // Function level variable declaration
fmt.Println(i, c, python, java)
}2 Add initializer
You can add an initializer to each variable (initializer):
var Variable name 1, Variable name 2 Variable type = The initializer 1, The initializer 2Note that the number of initializers must be the same as the number of variables . With initializer , Variable types can be omitted , The type of this variable is automatically inferred from the initializer . Example :
package main
import "fmt"
var i, j int = 1, 2
func main() {
var c, python, java = true, false, "no!" // With initializer , Variable types can be omitted
fmt.Println(i, j, c, python, java)
}3 Simplify variable declarations
Within the function , When implicit type declarations are used ( There is an initializer , Omit variable type ), Use simpler statements :
Variable name := The initializer Example :
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)
}Be careful : Outside the function , Every statement must use the keyword (var, func etc. ) Start , So it can't be used := Structure .
4 Declare variables in chunks
Variables can be declared as a block , Make the structure more clear :
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 Basic types
go The basic types are :
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 complex128Integer numbers are generally used int type , Unless there is a special reason to use other byte lengths or unsigned integers . When there is no initializer , Default values for various types :
- Numeric type :0
- bool type :false
- string type :""
6 Type conversion
When the type of assignment is different from the type of variable , Type conversion required :
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)Or it can be simplified to :
i := 42
f := float64(i)
u := uint(f)Be careful go There is no implicit type conversion , Display type conversion must be used .
7 Type inference
When we use implicit type declarations , The variable type will be based on the value on the right side of the declaration statement ( The initializer ) Make inferences .
- When the value on the right is a variable of known type , The type of the variable on the left is the same :
var i int
j := i // j Also for the int type - When the value on the right is a numeric literal constant , Then the variable on the left is int, float64, or complex128 type .
i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128- When the value on the right is bool or string Literal measure time , The variables on the left are of corresponding types .
8 Constant type
The declaration of constants is similar to that of variables , Just will var Change the keyword to const, And can't use := Make a statement . Example :
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 Numerical constants
Numeric constants are high-precision values . When a numeric constant does not declare a type , Its type is not inferred from the value on the right like a variable , The type will be determined according to the context . Example :
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)) // Report errors :constant 1267650600228229401496703205376 overflows int
}reference : A tour of gogo Reference documents Go’s Declaration Syntax
边栏推荐
猜你喜欢

Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)

LEGO announces price increase, speculators are more excited

SFOD:无源域适配升级优化,让检测模型更容易适应新数据(附论文下载)

Six programming insights in these five years!

mysql 系列:总体架构概述

The team of China University of Mines developed an integrated multi-scale deep learning model for RNA methylation site prediction

他山之石 | 微信搜一搜中的智能问答技术

golang 重要知识:context 详解

Important knowledge of golang: timer timer

Gartner最新报告:低代码应用开发平台在国内的发展
随机推荐
WebService interface publishing and calling
重卡界销售和服务的“扛把子”,临沂广顺深耕产品全生命周期服务
电子学会图形化一级编程题解析:猫捉老鼠
Important knowledge of golang: rwmutex read / write lock analysis
2021-05-22
F5《2022年应用策略现状报告》:边缘部署及负载安全成亚太地区关注焦点
2021-06-03
2021-05-08
快速排序的简单理解
Volatile~ variables are not visible under multithreading
2021-04-15
Solution to the problem that MySQL cannot be started in xampp
How can genetic testing help patients fight disease?
PHP指定字段大于100正序排,小于100随机排
杀入美团、饿了么腹地,京东外卖劲儿有多大?
JS创建一个数组(字面量)
Google &huggingface| zero sample language model structure with the strongest ability
Important knowledge of golang: atomic atomic operation
How can genetic testing help patients fight disease?
AXI_ Round_ Robin_ Arbiter design - aw and W channels