当前位置:网站首页>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
边栏推荐
- Preliminary practice of niuke.com (11)
- MySQL 8.0 data backup and recovery
- Discussion Net legacy application transformation
- Golang type assertion and conversion (and strconv package)
- Basic command of IP address configuration ---ip V4
- The 29th day of force deduction (DP topic)
- Exercises of function recursion
- SQL injection - Fundamentals of SQL database operation
- Phpexcel import export
- Discussion Net legacy application transformation
猜你喜欢

Machine learning support vector machine SVM

Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths

TLS environment construction and plaintext analysis

Qt6 QML Book/Qt Quick 3D/基础知识

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

In 2021, the global revenue of thick film resistors was about $1537.3 million, and it is expected to reach $2118.7 million in 2028

2022 low voltage electrician examination and low voltage electrician simulation examination question bank

《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!...

Based on laravel 5.5\5.6\5 X solution to the failure of installing laravel ide helper

2.6 formula calculation
随机推荐
全网都在疯传的《老板管理手册》(转)
TLS environment construction and plaintext analysis
For in, foreach, for of
Camera calibration (I): robot hand eye calibration
How can the outside world get values when using nodejs to link MySQL
In 2021, the global revenue of thick film resistors was about $1537.3 million, and it is expected to reach $2118.7 million in 2028
How to handle wechat circle of friends marketing activities and share production and release skills
Implementation of stack
Preliminary practice of niuke.com (11)
MySQL master-slave synchronization principle
Golang type assertion and conversion (and strconv package)
thrift go
Phpexcel import export
Global and Chinese markets for medical temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
Strange way of expressing integers (expanding Chinese remainder theorem)
Producer consumer mode (multithreading, use of shared resources)
Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
QT6 QML book/qt quick 3d/ Basics
Wargames study notes -- Leviathan