This is a creation in Article, where the information may have evolved or changed.
I believe that when you look at the programming language Learning tutorial will inevitably feel a little dull:
- Read your own books and have trouble asking for help
- The code snippet in the book, the example is static
- Although sometimes the book will give a running output, but not intuitive
- It is also troublesome to copy the code to run the IDE.
- Switching between the IDE and the book, even if it's running, is still a hassle
- In short, programmers are lazy people
It's much more comfortable to watch and practice , and the sample code in the book is easier to understand and digest after being physically verified/fine-tuned.
Based on this starting point, the Sym community decided to give you dry goods:
- From the content: Choose the rain marks of "Go learning Notes" as a tutorial, the book is concise, explain the go key points
- From the technical: community integration of Go Black technology--wide, in the technical realization of the side to see and practice !
Even if you have read the book before, we strongly recommend you to look at it here again, because it will bring a completely different experience ~
The light says does not practice false Bashi, the cargo!
1.1 Variables
Go is a statically typed language and cannot be changed at run time by variable type.
Use keywords var
to define variables, automatically initialized to 0 values. If you provide an initialization value, you can omit the variable type, which is automatically inferred by the compiler.
var x intvar f float32 = 1.6var s = "abc"
Inside the function, you can define a variable in a more abbreviated ": =" manner.
func main() { x := 123 // 注意检查,是定义新局部变量,还是修改全局变量。该方式容易造成错误。}
You can define multiple variables at once.
When assigning multiple variables, all related values are evaluated first and then assigned from left to right.
data, i := [3]int{0, 1, 2}, 0 i, data[i] = 2, 100
Special write-only variable "_" for ignoring value placeholders.
The compiler uses unused local variables as errors.
var s string // 全局变量没问题。func main() { i := 0 // Error: i declared and not used。(可使用 "_ = i" 规避)}
Note the difference between the re-assignment and the definition of the new variable with the same name.
1.2 Constants
The constant value must be a number, a string, and a Boolean value that can be determined at compile time.
const x, y int = 1, 2 // 多常量初始化const s = "Hello, World!" // 类型推断const ( // 常量组 a, b = 10, 100 c bool = false)func main() { const x = "xxx" // 未使⽤用局部常量不会引发编译错误。}
Type suffixes such as 1UL and 2LL are not supported.
In a constant group, if the type and initialization values are not provided, it is treated the same as the previous constant.
const ( s = "abc" x // x = "abc")
A constant value can also be a len
cap
unsafe.Sizeof
function return value, such as the compile period, which determines the result.
const ( a = "abc" b = len(a) c = unsafe.Sizeof(b))
If the constant type is sufficient to store the initialization value, no overflow error is thrown.
const ( a byte = 100 // int to byte b int = 1e20 // float64 to int, overflows)
Enumeration
The keyword iota
defines the self-increment enumeration value in a constant group that starts at 0 by the row count.
const ( Sunday = iota // 0 Monday // 1,通常省略后续⾏行表达式。 Tuesday // 2 Wednesday // 3 Thursday // 4 Friday // 5 Saturday // 6)const ( _ = iota // iota = 0 KB int64 = 1 << (10 * iota) // iota = 1 MB // 与 KB 表达式相同,但 iota = 2 GB TB)
In the same constant group, you can provide more than one iota
, each of which grows.
const ( A, B = iota, iota << 10 // 0, 0 << 10 C, D // 1, 1 << 10)
If iota
the self-increment is interrupted, an explicit recovery is required.
const ( A = iota // 0 B // 1 C = "c" // c D // c,与上⼀一⾏行相同。 E = iota // 4,显式恢复。注意计数包含了 C、D 两⾏行。 F // 5)
You can implement enumeration type restrictions by using custom types.
Next: Go to watch while practicing-"Go study Notes" series (ii)
- This series is based on the rain marks of "Go Learning Notes" (fourth edition) collation compiled, thank you very much for the rain scar hard to pay and share!
- Reprint Please specify: The article reprinted from: Hacker and Painter's Community [http://symphony.b3log.org]
- If you think this chapter is doing well, please enjoy it below!