当前位置:网站首页>2. Basic knowledge of golang
2. Basic knowledge of golang
2022-07-07 15:16:00 【Indulge fly】
Catalog
3. Naming specification ( Suggest )
1. identifier
identifier : Just name it , The composition rules are as follows
1. Identifiers are made up of letters , Numbers and underscores
2. Cannot start with a number
3. Identifiers are case sensitive 2. keyword
1. Keywords are built-in characters , Cannot be used as an identifier
2. common 25 Two keywords and 36 Predefined identifiers
3. Don't have to remember , You'll get familiar with it slowly 3. Naming specification ( Suggest )
1.Go It's case sensitive language
2. When an identifier is referenced as an external package , The first letter should be capitalized
When the identifier is not visible by outsourcing , Initial lowercase
3. Package name :
Any one of them go Files can be referenced as packages ,
After naming the package name (package Package name ) when , Try to keep consistent with the directory , Or meaningful
4.go File naming : A lowercase letter + Underline
5. Structure naming : Hump nomenclature
6. The interface name : With "er" ending
7. Constant naming : All capitals + Underline
8. unit testing : The file is named with "_test.go" ending , The function name is "Test" start 4. Variable
1. The variable of reputation cannot be repeated , And after the declaration, you must use
2. Externally declared variables :var Variable name Variable type
3. Batch variable declaration :
var(
name string
sex string
age int
)
4. Variable initialization : use = assignment
var name = "fly"
5. Variable type derivation : During initialization , Don't declare data types ,go Internal will automatically deduce
var(
name = "fly"
age = 18
sex = "man"
)
6. Initialize multiple variables : Separated by commas , One to one assignment
var name, age, sex = "fly", 18, "man"
7. Short variable declaration : Refers to variables declared inside a function , You don't have to write var, use ":="
fun main() {
name := "zcy"
age = 18
sex = "man"
}
8. Anonymous variables : When receiving variables as parameters , Unused variables are underlined "_" Instead of
package main
import "fmt"
func getNameAge() (string, int) {
return "fly", 18
}
func main() {
name, _ := getNameAge()
fmt.Printf("name: %v\n", name)
}
9. notes :
Single-line comments : Double diagonal bar // ctrl + /
Multiline comment :/* */ alt + shif t+ a5. Constant
1. Concept : Constants are defined in the compilation stage , Unalterable
2. Constant definition : Use keywords const
usage :const Constant names type = Constant values
3. Because when defining constants, you must assign values , So constant types can be omitted
4. Batch definition constants
const (
width = 100
height = 100
)
5. When defining constants in batch , If the following constant omits the assignment , The default is the same as the previous variable
const (
hight = 100 // 100
weidth // 100
name = "fly" // fly
age // fly
)
6. Continuous definition ( Multiple assignments ):
const width, height = 100, 100
7.iota
(1)iota Is a constant that can be modified by the compiler , The default is 0, Every time you call +1, encounter const When the reset to 0
(2) Both constants define iota, There is a gap between the two "_" Or ordinary constants ,iota The value of will still jump
const (
a = iota // 0
_ // encounter "_",iota still +1, here =1
b = iota // 2
c = 1 // 1 Encountered a common variable ,iota still +1, here =3
d = iota // 4
)6. Data type classification
1. Boolean type
2. Numeric type ( integer , floating-point )
3. String type ( Fixed length )
4. Derived type ( Pointer types , An array type , Structured type ,Channel type , Function type , Slice type , Interface type (interface),Map type )7. Basic supplement
1. Judge if
if Conditions {
The body of the function
} else {
The body of the function
}
2. loop for
for The initial state ; Conditions ; State change {
The body of the function
}
3. And :&&
4. Print variables
fmt.Println( Variable )
5. Format printing
fmt.Printf("%v\n", Variable )
difference :Println Bring your own line feed
5. Print symbols
Print data :%v
fmt.Printf("%v\n", "FLY") // FLY
Print data type :%T
fmt.Printf("%T\n", "fly") // string
Print number type integer size :%dB
fmt.Printf("%dB\n", 18) // 18B8. Boolean type
true,false
effect : In conditional judgment , loop , Logical expression, etc , Used to judge true or false
Be careful :golang Boolean type in , You can't use numbers (0 Or not 0) Express true and false
package main
import "fmt"
func main() {
age = 200
// error
if age {
fmt.Println(" error ")
}
// correct
if age>1 {
fmt.Println(" correct ")
}
}9. Numeric type
1. Include : Integer and floating point , Primitive plural , Bit operation uses complement
2. integer :int integer ,uint Unsigned integer ,uintptr Unsigned integer with fixed length to store pointer
3.int And uint stay 32 Bit on the operating system 4 Bytes , stay 64 Bit on the operating system 8 Bytes
4.go The sizes of the types in are fixed ( understand ), among int Type is the fastest kind of calculation
5. floating-point :float32 And float64( Add : Floating point numbers in computers are imprecise !)
(1) The integer zero value is 0, Floating point zero is 0.0
(2) Format and print floating point :
Full print :%f
Take two digits to print :%.2f
6. Hexadecimal conversion Binary format output
Decimal system :%d
Binary system :%b
octal :%o
Hexadecimal :%x or %X
7. The plural :
32 position :complex64
64 position :complex128边栏推荐
- Ctfshow, information collection: web12
- Ctfshow, information collection: web13
- What are the safest securities trading apps
- 一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
- CTFshow,信息搜集:web14
- Stream learning notes
- STM32F103C8T6 PWM驱动舵机(SG90)
- Niuke real problem programming - Day9
- 【OBS】RTMPSockBuf_Fill, remote host closed connection.
- 简述keepalived工作原理
猜你喜欢

写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜

Niuke real problem programming - day15

如何在opensea批量发布NFT(Rinkeby测试网)

With 8 modules and 40 thinking models, you can break the shackles of thinking and meet the thinking needs of different stages and scenes of your work. Collect it quickly and learn it slowly

Ctfshow, information collection: web14

拜拜了,大厂!今天我就要去厂里

数学建模——什么是数学建模

什么是数据泄露

【OBS】RTMPSockBuf_Fill, remote host closed connection.

Ctfshow, information collection: web8
随机推荐
Mathematical modeling -- what is mathematical modeling
2022年5月互联网医疗领域月度观察
[target detection] yolov5 Runtong voc2007 data set
CTFshow,信息搜集:web2
[server data recovery] a case of RAID data recovery of a brand StorageWorks server
CTFshow,信息搜集:web6
时空可变形卷积用于压缩视频质量增强(STDF)
用于增强压缩视频质量的可变形卷积密集网络
Niuke real problem programming - day18
CTFshow,信息搜集:web13
[机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
Ctfshow, information collection: web13
Niuke real problem programming - Day11
Ctfshow, information collection: web2
TypeScript 发布 4.8 beta 版本
Ctfshow, information collection: web9
Stream learning notes
asp. Netnba information management system VS development SQLSERVER database web structure c programming computer web page source code project detailed design
使用Scrapy框架爬取网页并保存到Mysql的实现
STM32F103C8T6 PWM驱动舵机(SG90)