<>GO Language learning notes
I'm learning go Some notes when speaking English , Hope to learn and progress with the big guys !
<>1, Basic grammar
go The basic structure of language
package main import ( "fmt" ) func main(){ fmt.Println("hello wolrd")
// Pay attention to the output Println,P Be capitalized }
To learn a language, one must first learn how to annotate it
package main import ( "fmt" ) func main(){ // Single-Line Comments
// Add spaces between variables and operators , Make the program look more beautiful , as :fruit = apples +oranges; /* many that 's ok notes Buddhism */ }
stay go Language code can not be added at the end “;”
But two lines of code written on the same line must have ";"( Put in LiteIDE It will automatically become Two lines of code , Other compilers are unclear ) Generally not recommended .
// Not recommended ↓ fmt.Println("hello wolrd");fmt.Println("hello wolrd2") // Recommended ↓ fmt.
Println("hello wolrd") fmt.Println("hello wolrd2")
stay go Use in language fmt.Sprintf Format the string and assign it to the new string
var stockode = 123 var enddate ="2020-12-31" var url="Cod=%d&endDate=%s" var
target_url= fmt.Sprintf(url, stockcode, enddate) fmt.Println(target_url)
<>2, variable
<>1, Declare variables
// Declaration method 1 var a string = "abc" // Declaration method 2 var a string a ="abc" // example : package main
import ( "fmt" ) func main(){ var a string = "abc" fmt.Println(a) }
Specifies the variable type , If not initialized , The variable defaults to zero
var a = "abc" var b int var c bool fmt.Println(a, b, c) /*
value type ( include complex64/128) by 0 Boolean type is false The string is ""( Empty string ) The following types are nil: var a *int var
a []int var a map[string] int var a chan int var a func(string) int var a error
// error It's the interface */
<>2, No, var Variable declaration for
use : = To declare
Appear in : = The variables on the left should not have been declared , Otherwise, it will cause compilation errors
a := 1
Here is an example of a mistake :
var a int// There is no need to declare it here. If you declare it, you will report an error a:=10 fmt.Println(a)
//:= It's a statement in itself , therefore := The variable name used must be undeclared before, otherwise an error will be reported
This is the right one
a := 1 fmt.Println(a) b := "abc" //var b string="abc" fmt.Println(b)
<>3, Multivariable declaration
var a, b int a, b = 1, 2 //a,b := 1,2 fmt.Println(a, b)
Either way is OK
var a,b int =1,2 fmt.Println(a,b)
<>3, constant
<>1, declare constant
Constant can be used len(), cap(), unsafe.Sizeof() Function evaluates the value of an expression . In constant expressions , Function must be built-in , Otherwise, it will not compile :
const a string = "abc" fmt.Println(a) const b = "abc" // println(b) fmt.Println
(b) // fmt.Printf(b)
<>2, Special constant iota
iota, Special constant , It can be considered as a constant that can be modified by the compiler .
first iota be equal to 0, whenever iota When a new line is used , Its value will be automatically increased 1
iota stay const When the keyword appears, it will be reset to 0(const Before the first line inside ),const Each new line of constant declaration in the iota Count once (iota
It can be understood as const Row index in statement block ).
const ( a = "na" b = iota //0 c //1 d = 0 // Independent value ,iota+1 e //0 iota+1 f = iota
//4 g //5 ) fmt.Println(a, b, c, d, e, f, g) // test iota var h int = 0 h = c+ f fmt
.Println(h)
Technology
Daily Recommendation