A quick introduction to basic grammar in the Go language introduction tutorial
- 2020-05-10 18:20:01
- OfStack
The Go language is an open source language designed to create simple, fast, and reliable software.
An example tutorial on the Go language, illustrating the use of the Go language with examples and comments.
Hello World
The first program will output the "hello world" message. The source code is as follows:
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
// through go run To run the Go The program
$ go run hello-world.go
hello world
// It will take 1 Time to compile the source code 2 Base files can be passed go build Achieve this 1 Process.
$ go build hello-world.go
$ ls
hello-world hello-world.go
// And then just run it 2 A binary file
$ ./hello-world
hello world
Values: value type
Go has many value types including: strings, integers, floats, booleans, and so on. Here are some basic examples.
package main
import "fmt"
func main() {
// Strings can be used + To connect to
fmt.Println("go" + "lang")
// Integer and floating point Numbers
fmt.Println("1+1 =", 1+1)
fmt.Println("7.0/3.0 =", 7.0/3.0)
// Boolean types, you can use Boolean operators
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(!true)
}
$ go run values.go
golang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false
Variables: variable
In Go, variables are explicitly declared; The editor can check for type correctness in function calls.
package main
import "fmt"
func main() {
// with var The statement 1 Two or more variables
var a string = "initial"
fmt.Println(a)
// You can declare multiple variables
var b, c int = 1, 2
fmt.Println(b, c)
//GO The default value is used to determine the variable type
var d = true
fmt.Println(d)
// A variable with a type but no value is assigned zero value zero-valued. int The value of type zero is 0.
var e int
fmt.Println(e)
//:= Language is shorthand for declaring and initializing variables , For example,
// And: var f string = "short" equivalent
f := "short"
fmt.Println(f)
}
$ go run variables.go
initial
1 2
true
0
short
Constants: constant
Constants supported by Go are types character, string, boolean, and numeric.
package main
import "fmt"
import "math"
// Constant use const The statement
const s string = "constant"
func main() {
fmt.Println(s)
//const Statements can be like var1 The sample is used anywhere
const n = 500000000
// A constant can represent any precision
const d = 3e20 / n
fmt.Println(d)
// Numeric constants have no type until assigned, as specified by the following display
fmt.Println(int64(d))
// A number can specify the type required in the context, for example math.Sin Need to be 1 a float64 The type of
fmt.Println(math.Sin(n))
}
$ go run constant.go
constant
6e+11
600000000000
-0.28470407323754404
For: circulation
for is the 1-only cyclic structure in the Go language. The following are the basic three types of loops.
package main
import "fmt"
func main() {
// The most basic type, 1 A single 1 The cycle of
i := 1
for i <= 3 {
fmt.Println(i)
i = i + 1
}
//1 Type: with initialization / Loop of conditions
for j := 7; j <= 9; j++ {
fmt.Println(j)
}
// conditioned for The cycle will 1 Execute until you break or return in a closure.
for {
fmt.Println("loop")
break
}
}
$ go run for.go
1
2
3
7
8
9
loop
There is one other use of for that we will see in the range syntax.
If/Else: conditional statement
Go implements conditional branching through if and else
package main
import "fmt"
func main() {
//1 Three basic USES
if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}
// only if In the case
if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}
// You can declare variables in conditions; Any declaration can be used in all conditional code snippets
if num := 9; num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
}
The Go language does not have a 3-byte (ternary if) operator, you need to use if
$ go run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit
Switch: conditional enumeration
The Switch syntax can implement multiple conditional branches.
package main
import "fmt"
import "time"
func main() {
// Here is the 1 A basic Switch
i := 2
fmt.Print("write ", i, " as ")
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
}
// You can use multiple conditional matches, and you can also use default matches
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println("it's the weekend")
default:
fmt.Println("it's a weekday")
}
// unconditional switch Can be implemented if/else Effect of type
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("it's before noon")
default:
fmt.Println("it's after noon")
}
}
$ go run switch.go
write 2 as two
it's the weekend
it's before noon