当前位置:网站首页>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
边栏推荐
- XSS prevention
- 集成底座方案演示说明
- 近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
- The 8th Blue Bridge Cup single chip microcomputer provincial competition
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
- Failed to upgrade schema, error: “file does not exist
- 0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
- 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
- [tips] use Matlab GUI to read files in dialog mode
- 【DesignMode】建造者模式(Builder model)
猜你喜欢

The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup

毕设-基于SSM电影院购票系统

Flutter中深入了解MaterialApp,常用属性解析
![[designmode] Prototype Pattern](/img/ee/c4e48c2ce8ff66f50f0bf13e5a0418.png)
[designmode] Prototype Pattern

BiShe cinema ticket purchasing system based on SSM

MySQL index, transaction and storage engine

Demonstration description of integrated base scheme

Wpviewpdf Delphi and Net PDF viewing component

Jetpack之LiveData扩展MediatorLiveData

Jetpack's livedata extension mediatorlivedata
随机推荐
High performance and low power cortex-a53 core board | i.mx8m Mini
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
Account management of MySQL
毕设-基于SSM电影院购票系统
Class design basis and advanced
SQL:常用的 SQL 命令
Which product of anti-cancer insurance is better?
Oracle 常用SQL
Basic syntax of unity script (7) - member variables and instantiation
XSS prevention
The 5th Blue Bridge Cup single chip microcomputer provincial competition
Influence of air resistance on the trajectory of table tennis
Unity脚本的基础语法(6)-特定文件夹
一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
WPViewPDF Delphi 和 .NET 的 PDF 查看组件
Demonstration description of integrated base scheme
Suggestions on settlement solution of u standard contract position explosion
[designmode] Prototype Pattern
5G時代全面到來,淺談移動通信的前世今生
regular expression