当前位置:网站首页>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边栏推荐
猜你喜欢

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

CTFshow,信息搜集:web14

【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码

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

How bad can a programmer be? Nima, they are all talents

Wechat applet 01

Niuke real problem programming - Day17

Xiaomi's path of chip self-development

上半年晋升 P8 成功,还买了别墅!

如何在opensea批量发布NFT(Rinkeby测试网)
随机推荐
暑期安全很重要!应急安全教育走进幼儿园
TypeScript 发布 4.8 beta 版本
数学建模——什么是数学建模
居然从408改考自命题!211华北电力大学(北京)
[make a boat diary] [shapr3d STL format to gcode]
Niuke real problem programming - Day11
MySQL bit type resolution
Connecting FTP server tutorial
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
Bits and Information & integer notes
How does the database perform dynamic custom sorting?
Comparable and comparator of sorting
CTFshow,信息搜集:web1
8大模块、40个思维模型,打破思维桎梏,满足你工作不同阶段、场景的思维需求,赶紧收藏慢慢学
使用Scrapy框架爬取网页并保存到Mysql的实现
如何在opensea批量发布NFT(Rinkeby测试网)
[target detection] yolov5 Runtong voc2007 data set
“百度杯”CTF比赛 2017 二月场,Web:include
What is the process of ⼀ objects from loading into JVM to being cleared by GC?
【数据挖掘】视觉模式挖掘:Hog特征+余弦相似度/k-means聚类