当前位置:网站首页>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
边栏推荐
- Q&A:Transformer, Bert, ELMO, GPT, VIT
- Qtablewidget control of QT
- 浅析 Ref-NeRF
- 18、 MySQL -- index
- An old programmer gave it to college students
- Apprentissage intensif - notes d'apprentissage 1 | concepts de base
- Preliminary practice of niuke.com (11)
- QT6 QML book/qt quick 3d/ Basics
- 【leetcode】1027. Longest arithmetic sequence (dynamic programming)
- 1.5 learn to find mistakes first
猜你喜欢

An old programmer gave it to college students

Shortest path problem of graph theory (acwing template)

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

Discussion Net legacy application transformation

强化學習-學習筆記1 | 基礎概念

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

The 29th day of force deduction (DP topic)

JVM JNI and PVM pybind11 mass data transmission and optimization

MySQL master-slave synchronization principle

Example of peanut shell inner net penetration
随机推荐
Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
Machine learning support vector machine SVM
Rad+xray vulnerability scanning tool
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
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
How to read the source code [debug and observe the source code]
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
不同业务场景该如何选择缓存的读写策略?
Thread, thread stack, method stack, the difference of creating thread
Implementation of stack
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%
C 10 new feature [caller parameter expression] solves my confusion seven years ago
Plan for the first half of 2022 -- pass the PMP Exam
Commands related to files and directories
AcWing 1460. Where am i?
浅析 Ref-NeRF
Sword finger offer 30 Stack containing min function
Producer consumer mode (multithreading, use of shared resources)
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