当前位置:网站首页>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 int
Definition , 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 214
e
With it outsidee
Is different , Although there is already a variable outsidee
But it's not:=
Or create a new variable in the anonymous functione
.
Welcome to my WeChat official account. Notes on Jiangda
边栏推荐
- Exercises of function recursion
- 4. Data splitting of Flink real-time project
- Sword finger offer 30 Stack containing min function
- 浅议.NET遗留应用改造
- thrift go
- Battle drag method 1: moderately optimistic, build self-confidence (1)
- Wireless network (preprocessing + concurrent search)
- How can the outside world get values when using nodejs to link MySQL
- Discussion Net legacy application transformation
- jvm jni 及 pvm pybind11 大批量数据传输及优化
猜你喜欢
How to modify the network IP addresses of mobile phones and computers?
QT6 QML book/qt quick 3d/ Basics
强基计划 数学相关书籍 推荐
强化学习-学习笔记1 | 基础概念
44. Concurrent programming theory
Do you really know how old you are?
Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
Change deepin to Alibaba image source
AcWing 1460. Where am i?
[Tang Laoshi] C -- encapsulation: member variables and access modifiers
随机推荐
19、 MySQL -- SQL statements and queries
Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
[effective Objective-C] - block and grand central distribution
Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
Upgrade PIP and install Libraries
thrift go
Kubernetes abnormal communication network fault solution ideas
Gauss elimination solves linear equations (floating-point Gauss elimination template)
Basic knowledge of dictionaries and collections
Thread, thread stack, method stack, the difference of creating thread
Reinforcement learning - learning notes 1 | basic concepts
强化學習-學習筆記1 | 基礎概念
Introduction to golang garbage collection
Use of CMD command
Deep search DFS + wide search BFS + traversal of trees and graphs + topological sequence (template article acwing)
4. Data splitting of Flink real-time project
47. Process lock & process pool & Collaboration
Global and Chinese market of charity software 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
AcWing 1460. Where am i?