Create a hello directory for the first Go source code.
1 2
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.mod file 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 init command, 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.
But for the purposes of just a tutorial, just use example/hello.
1 2
$ 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.
1 2 3 4 5 6 7
package main
import"fmt"
funcmain(){ 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 fmt package, 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 main function to print a message to the console. A main function executes by default when you run the main package.
Run the code to see the greeting.
1 2
$ 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.mod file 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 init command, 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 main function executes by default when you run the main package.
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
funcmain() { 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 = ""这种方式来声明变量,声明变量的时候,一般会自动去推导变量的类型。
funcmain() { 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 } }