当前位置:网站首页>Go learning notes (4) basic types and statements (3)
Go learning notes (4) basic types and statements (3)
2022-07-03 20:40:00 【Raring_ Ringtail】

Today is Valentine's Day , All shall be well, Jack shall have jill ️.
Continue to learn go Well , These articles I wrote go Study notes are not suitable for beginners go Classmate , Because they are all concerned about the details of the language .
I quite understand in general go After how to use and write, look back at the details that were ignored at the beginning . Beginners go It is suggested to see the official tutorial:https://go.dev/doc/tutorial/getting-started
string、rune
string yes go Built in type , Its zero value is an empty string ,string It's immutable .go Support Unicode .
string It can be used == Judge whether it is equal , use != Judge whether it is not equal , use >,>=,<,<= To compare the size .
string Support with + Splicing , Although with + Splicing strings is very convenient , But it's very slow , If there are a large number of strings to be spliced, it will make you wonder whether the computer crashed .
If you want to splice a large number of strings , It can be used strings.Builder:
var builder strings.Builder
strs := []string{
"Every moment", " of each day,",
" loving and", " missing you ", "dominates ",
"every inch of ", "my brain. "}
for _, str := range strs {
builder.WriteString(str)
}
builder.WriteString("\n")
fmt.Println(builder.String())
Output :Every moment of each day, loving and missing you dominates every inch of my brain.
rune Type is used to represent various characters , I told you last time ,rune Namely int32 An alias for .
Explicit type conversion
go The type conversion of is similar to other languages , use type ( Variable ), You can convert the variable to another type .go Implicit type conversion is not supported , If two variables of different types operate on each other, errors will occur .
var x int = 200
var y int32 = 14
var z int64 = int64(x)+int64(y)
var a float32 = 2.1
var b float64 = 0.04
var c float64 = float64(a) + b
var m int64 = z + int64(c)
fmt.Println(z)
fmt.Println(c)
fmt.Println(m)
Output :
214
2.1399999046325684
216
because go Only explicit type conversion is supported , So other types cannot be used as Boolean types , If you want to convert other types to Boolean types , Then we can only get (==,!=,>,<,<= or >=)
var And :=
go Although it is a strongly typed static language , But when writing, there is always the illusion that this is a dynamic language .
Unlike most other languages ,go When declaring variables, the name of the variable will be put in front , And put the type of variable behind .
I'm not used to it at first , But over time, I found that this will make the code cleaner and easier to read .
The most basic way to declare variables is :
var x int // x Will be automatically initialized to 0
var x int = 214 // x To be an assignment 214
If the type of the variable is the same as the default type of the literal to be assigned, you don't need to write the type when declaring .
var x = 214
Note here , although go Implicit conversion of variables is not supported , But for literal quantity, it can be considered that it supports implicit conversion , Or think that the literal type is determined only when it is assigned ( It's reasonable to think about it ,214 At first glance, you will think it is int, But you must say it's a float64 No problem ).
Declare more than one variable at a time :
var x, y int
// Define multiple variables of the same type
var x, y int = 2, 14
// Define multiple variables of different types
var a, b = 214, "Valentine's Day"
var (
x int
y = 2
z int = 14
d, e = 214, "Valentine's Day"
f, g string
)
In addition to using var There is also a more lazy way to define variables :=:
var x = 214
x := 214
var x, y = 214, "Valentine's Day"
x, y := 214, "Valentine's Day"
Is it more and more like dynamic language , It's so convenient , This is the most commonly used way to define variables .
:= It can only be used to define new variables , It is not assignment , If the variable has been declared , Reuse := Will compile incorrectly . With one exception , If := When the left side contains both undeclared and declared variable names , Then it will define undeclared variables , And assign a value to the declared variable :
x := 214
x, y := 1314, "Valentine's Day"
Some are not suitable := The place of
- It is best to use
var x intDefinition , In this way, the semantics are the most clear - When defining variables, don't use if the default type of literal is different from what you want
- When you want to reuse variables in the external scope and create new variables at the same time , It is likely that the variable you want to reuse will also be created as a variable with the same name , My expression is more convoluted . Look at examples :
Output :var e float64 = 214 func() { n, e := 214, 214.214 fmt.Println(n, e) }() fmt.Println(e)
go It supports closures , In this case , In anonymous functions214 214.214 214eWith it outsideeIs different , Although there is already a variable outsideeBut it's not:=Or create a new variable in the anonymous functione.
Welcome to my WeChat official account. Notes on Jiangda
边栏推荐
- Derivation of decision tree theory
- How to set the system volume programmatically- How to programmatically set the system volume?
- String and+
- LabVIEW training
- 2166. Design bit set
- Qtablewidget control of QT
- Introduction to golang garbage collection
- 《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!...
- TLS environment construction and plaintext analysis
- 强基计划 数学相关书籍 推荐
猜你喜欢

Rhcsa third day notes

Gee calculated area

Etcd 基于Raft的一致性保证

强化学习-学习笔记1 | 基础概念

An old programmer gave it to college students

如临现场的视觉感染力,NBA决赛直播还能这样看?

In 2021, the global foam protection packaging revenue was about $5286.7 million, and it is expected to reach $6615 million in 2028

【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装

2022 safety officer-c certificate examination and safety officer-c certificate registration examination

Node MySQL serialize cannot rollback transactions
随机推荐
Based on laravel 5.5\5.6\5 X solution to the failure of installing laravel ide helper
Refer to some books for the distinction between blocking, non blocking and synchronous asynchronous
Get log4net log file in C - get log4net log file in C
47. Process lock & process pool & Collaboration
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
电子科技大学|强化学习中有效利用的聚类经验回放
JVM JNI and PVM pybind11 mass data transmission and optimization
【leetcode】1027. Longest arithmetic sequence (dynamic programming)
App compliance
[Tang Laoshi] C -- encapsulation: member variables and access modifiers
It is discussed that the success of Vit lies not in attention. Shiftvit uses the precision of swing transformer to outperform the speed of RESNET
XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向
Camera calibration (I): robot hand eye calibration
[raid] [simple DP] mine excavation
4. Data splitting of Flink real-time project
Q&A:Transformer, Bert, ELMO, GPT, VIT
Test access criteria
Test changes in Devops mode -- learning and thinking
Phpexcel import export
thrift go