当前位置:网站首页>Go variables and constants
Go variables and constants
2022-07-02 03:54:00 【UPythonFish】
List of articles
go Variables and constants
What is a variable
Variable specifies a storage location (Memory Location) The name of , The storage unit stores certain types of values . stay Go in , There are multiple syntax for declaring variables .
Variable defined 4 Ways of planting
func main() {
// 1. Complete definition var Variable name Variable type = A variable's value
var name string = "yxh"
var age int = 20
fmt.Println(name,age)
// 2. Type derivation var Variable name = A variable's value
var name = "yxh"
var age = 20
fmt.Println(name,age)
// 3. Make a brief statement Variable name := A variable's value
name := "yxh"
age := 20
fmt.Println(name,age)
// 4. Define multiple variables at the same time
// 4.1 Use one line definition
var name, gender string
var name, age, gender = "yxh", 20, " male "
name, age, gender := "yxh", 20, " male " // Recommended
// 4,2 understand
var (
name = "yxh"
age = 20
gender = " male "
)
var (
name string
age int
gender string
)
fmt.Println(name,age,gender)
}
Variable definition considerations :
1. After variable declaration type , It can only be modified with values of the same type
2. Short declarations can only be used inside functions , Cannot be used externally
3. Variables must be defined first , In the use of , And can only be defined once , You can only define without assignment
var age =19 // Defines a variable age
name,age :="lqz",20 // Defined variables name, Can write age, Said the use of , Modified value , It's not defined , in addition to , Nothing else
4. Variables declare that you must use , Otherwise, an error will be reported
5. Define only without assignment , Only fully defined schemes can be used
The type of variable
// Basic data type
- Numbers The following numbers represent the number of a number bit, And range size
- integer
-int // Sub platform , stay 32 Bit machine , Express int32, stay 64 Bit machine means int64
-int8 // Occupy 8 individual bit, The numerical range is ±2 Of 7 The power minus 1 -128 —— 127
-int16 // Plus or minus 2 Of 15 Power -1
-int32 // Plus or minus 2 Of 31 Power -1
-int64 // Plus or minus 2 Of 63 Power -1
- Positive integer : Unsigned integer
-uint // stay 32 Bit machine is uint32, stay 64 Bit machine is uint64
-uint8 // Occupy 8 individual bit, The numerical range is 2 Of 8 Power -1 0 —— 256
-uint16 // 2 Of 16 Power -1
-uint32 // 2 Of 32 Power -1
-uint64 // 2 Of 63 Power -1
- floating-point
-float32 // Accurate to the decimal point 6 position
-float64 // Accurate to the decimal point 15 by
- The plural ( Not used in normal development )
-complex128
-complex64
- character string
-string
- Double quotes " " or The quotation marks ` ` The parcel Backquotes indicate multiline strings , Support line break
Single quotation marks don't work : Single quotation marks indicate numbers , And only one character can be placed in single quotation marks
fmt.Println('a') // 97 a Character Unciode code
fmt.Println(' you ') // 20320 Your character Unicode code
- Boolean
-bool
-true false ( Are all lowercase )
-byte:uint8 Another name for
-rune:int32 Another name for
// Composite data type
- Array
- section
-map
// Advanced data types
- Custom type
- Function type
- Pointer types
// There are two ways to view data types
fmt.Println(reflect.TypeOf(nameOfYxh))
fmt.Printf("%T", nameOfYxh) // This can only be fmt.Printf() Use in
Type conversion
Go It has a very strict strong typology .Go There is no automatic type promotion or type conversion . You need to perform display conversion to perform corresponding operations , Columns such as
package main
import (
"fmt"
)
func main() {
i := 55 // int
j := 67.8 // float64
sum := i + j // Don't allow int + float64
sum := i + int(j) // allow
j = "yxh" // Don't allow , Report errors
fmt.Println(sum)
}
Add : stay python in int , float Wait is not the basic data type , But objects , And variable names point to memory addresses , It's not worth it , therefore python The variable name in is simple , And type conversion is also very convenient
Constant and iota
Constant , A constant quantity , stay go Once the assignment is defined in , Can't change anymore , And in the python Usually, variables in all uppercase are set as constants
The definition and use of constants
package main
import "fmt"
func main() {
// 1 The definition and use of constants
// Constant definition format const keyword Constant names type = value perhaps const keyword Constant names = value
const name string= "yxh"
// No modification allowed
name="lxx" // Report errors
fmt.Printf(name)
// Define multiple constants at the same time
const name ,age= "yxh",20
const (
name ="yxh"
age=20
)
// 2 iota: Self increasing constant
const (
a =1
b // 1 If the assignment is not written , Follow the assignment above amount to b=1
c // 1
d // 1
)
fmt.Println(a,b,c,d) // 1 1 1 1
const (
a = 1
b // 1 If the assignment is not written , Follow the assignment above amount to b=1
c = 99
d // d = 99
)
fmt.Println(a,b,c,d) // 1 1 99 99
const (
a =iota // Equate to a=0
b =iota // Self increasing
c =iota // Self increasing
d =iota // Self increasing
)
fmt.Println(a,b,c,d) // 0 1 2 3
const (
a =iota
b =100
c
d
e =iota // Every line will increase by itself
)
fmt.Println(a,b,c,d,e) // 0 100 100 100 4
const (
e, f, g = iota, iota, iota // As long as you don't change lines , It will not increase itself
h = iota
r = iota
)
fmt.Println(e, f, g, h, r) // 0 0 0 1 2
const (
a = iota + 1
b
c
)
fmt.Println(a, b, c) // 1 2 3
const (
a = 4 // Explicitly specify the value
b = 5 // Explicitly specify the value
c = iota // c = 2, Because of the iota Located at 3 individual ConstSpec,2=3-1
d = iota // d = 3, because iota It's increasing 1, Equivalent to d = iota
)
fmt.Println(a, b, c, d) // 4 5 2 3
const (
a =0 // a = 0
d=1
_ // Skip this ConstSpc, however iota Will add 1
b, c = iota, iota * 2 // iota=2
)
fmt.Println(a,b,c,d) // 0 3 6 1
// Define multiple constants at the same time , If the next line only writes the constant name , Follow the assignment method of the previous line
// iota Every new line , It's going to increase 1, Whether used or not
边栏推荐
- C language: examples of logical operation and judgment selection structure
- The 7th Blue Bridge Cup single chip microcomputer provincial competition
- Unity脚本的基础语法(6)-特定文件夹
- 蓝桥杯单片机第四届省赛
- What kind of interview is more effective?
- The 9th Blue Bridge Cup single chip microcomputer provincial competition
- 蓝桥杯单片机省赛第十二届第二场
- 蓝桥杯单片机省赛第十一届第一场
- The 11th Blue Bridge Cup single chip microcomputer provincial competition
- 一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
猜你喜欢
Didi open source Delta: AI developers can easily train natural language models
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
Basic operations of MySQL database (based on tables)
The 7th Blue Bridge Cup single chip microcomputer provincial competition
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
C language: examples of logical operation and judgment selection structure
Jetpack之LiveData扩展MediatorLiveData
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
随机推荐
Failed to upgrade schema, error: “file does not exist
JVM知识点
QT designer plug-in implementation of QT plug-in
Fingertips life Chapter 4 modules and packages
蓝桥杯单片机省赛第八届
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
SQL:常用的 SQL 命令
Interface debugging tool simulates post upload file - apipost
Xlwings drawing
The 9th Blue Bridge Cup single chip microcomputer provincial competition
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
SQL: common SQL commands
Blue Bridge Cup single chip microcomputer sixth temperature recorder
How to solve the code error when storing array data into the database
VS2010插件NuGet
Go语言介绍
Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
Unity脚本的基础语法(6)-特定文件夹