Work before Hello World
-
Open a command prompt and cd to home directory.
-
Create a hello directory for the first Go source code.
mkdir hello cd hello -
Enable dependency tracking for your code.
When your code imports packages contained in other modules, you manage those dependencies through your code's own module. That module is defined by a
go.modfile that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository.To enable dependency tracking for your code by creating a go.mod file, run the
go mod initcommand, giving it the name of the module your code will be in. The name is the module's module path. In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might begithub.com/mymodule. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module. For more about naming a module with a module path, see Managing dependencies. But for the purposes of just a tutorial, just useexample/hello.$ go mod init example/hello go: creating new go.mod: module example/hello -
In your text editor, create a file hello.go in which to write your code.
-
Paste the following code into your hello.go file and save the file.
package main import "fmt" func main(){ fmt.Println("Hello, World!") }This is your Go code. In this code, you:
- Declare a
mainpackage (a package is a way to group functions, and it's made up of all the files in the same directory) . - Import the popular
fmtpackage, which contains functions for formatting text, including printing to the console. This package is one of the standard library packages you got when you installed Go. - Implement a
mainfunction to print a message to the console. Amainfunction executes by default when you run themainpackage.
- Declare a
-
Run the code to see the greeting.
$ go run . Hello, World!
Key Points
- When your code imports packages contained in other modules, you manage those dependencies through your code's own module. That module is defined by a
go.modfile that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository. - To enable dependency tracking for your code by creating a go.mod file, run the
go mod initcommand, giving it the name of the module your code will be in. The name is the module's module path. - In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might be
github.com/mymodule. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module. For more about naming a module with a module path, see Managing dependencies. - Package is a way to group functions, and it's made up of all the files in the same directory.
- A
mainfunction executes by default when you run themainpackage. - go mod init command:
go mod init example/hello - go run command:
go run example/hello - go build command and run:
go build example/hello/main.go./example/hello/main
variable type
package main
import (
"fmt"
"math"
)
func main() {
var a = "initial"
var b, c int = 1, 2
var d = true
var e float64
f := float32(e)
g := a + "foo"
fmt.Println(a, b, c, d, e, f)
fmt.Println(g)
const s string = "constant"
const h = 500000000
const i = 3e20 / h
fmt.Println(s, h, i, math.Sin(h), math.Sin(i))
}
Key Points
-
go语言是一门强类型语言,每一个变量都有它自己的变量类型
-
go语言的字符串是内置类型,可以直接通过加号拼接,也能够直接用等于号去比较两个字符串。
-
go语言变量的声明有两种方式
- 一种是通过
var name string = ""这种方式来声明变量,声明变量的时候,一般会自动去推导变量的类型。 - 如果有需要,也可以显式写出变量类型。另一种声明变量的方式是:
使用变量 冒号 := 值。
- 一种是通过
-
常量就是把var改成const,值得一提的是go语言里面的常量没有确定的类型,会根据使用的上下文来自动确定类型。
if else
package main
import "fmt"
func main() {
if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}
if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}
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")
}
}
7 is odd
8 is divisible by 4
9 has 1 digit
Key Points
- if后面没有括号。
- if执行语句块必须接大括号。不能像C或者Cpp一样,直接把if里面的语句同一行。
for
package main
import "fmt"
func main() {
i := 1
for {
fmt.Println("loop")
break
}
for j := 7; j < 9; j++ {
fmt.Println(j)
}
for n := 0; n < 5; n++ {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
for i <= 3 {
fmt.Println(i)
i = i + 1
}
}
loop
7
8
1
3
1
2
3
Key Points
- go语言只有for循环
- 最简单的for后面什么也不写,是死循环
- for i等于0,i小于n,i加加。这中间三段,任何一段都可以省略。
switch
package main
import (
"fmt"
"time"
)
func main() {
a := 2
switch a {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("Three")
case 4, 5:
fmt.Println("four or five")
default:
fmt.Println("Other")
}
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("It's before noon")
default:
fmt.Println("It's after noon")
}
}
two
It's after noon
Key Points
- go语言的switch后面的那个变量名,不要括号。
- 很大的一点不同的是,在cpp里面,switch case如果不加break的话会然后会继续往下跑完所有的case,在go语言里面的话不加break也会跳出来。
- 相比C或者Cpp,go语言里面的switch功能更强大。可以使用任意的变量类型,甚至可以用来取代任意的if else语句。你可以在switch后面不加任何的变量,然后在case里面写条件分支。这样代码相比你用多个if else代码逻辑会更为清晰。