go语言hello world
在docker容器中安装go开发环境
go version
091606fc98de 容器ID
root@091606fc98de:/www/go_demo# go version
go version go1.17.5 linux/amd64
root@091606fc98de:/www/go_demo
root@091606fc98de:/www/go_demo# go version
go version go1.17.5 linux/amd64
root@091606fc98de:/www/go_demo# ls -al
total 4
drwxrwxrwx 1 root root 4096 Oct 23 09:46 .
drwxr-xr-x 3 root root 4096 Oct 23 09:38 ..
-rwxrwxrwx 1 root root 96 Oct 23 09:46 main.go
# bash
root@091606fc98de:/www/go_demo# go version
go version go1.17.5 linux/amd64
root@091606fc98de:/www/go_demo# ls -al
total 4
drwxrwxrwx 1 root root 4096 Oct 23 09:46 .
drwxr-xr-x 3 root root 4096 Oct 23 09:38 ..
-rwxrwxrwx 1 root root 96 Oct 23 09:46 main.go
root@091606fc98de:/www/go_demo# vim main.go
root@091606fc98de:/www/go_demo# cat main.go
package main
import "fmt"
func main() {
var a string = "Hello World"
fmt.Println(a)
}root@091606fc98de:/www/go_demo#
package main
import "fmt"
func main() {
var a string = "Hello World"
fmt.Println(a)
}
执行go run main.go 或 go run .
# bash
root@091606fc98de:/www/go_demo# go version
go version go1.17.5 linux/amd64
root@091606fc98de:/www/go_demo# ls -al
total 4
drwxrwxrwx 1 root root 4096 Oct 23 09:46 .
drwxr-xr-x 3 root root 4096 Oct 23 09:38 ..
-rwxrwxrwx 1 root root 96 Oct 23 09:46 main.go
root@091606fc98de:/www/go_demo# vim main.go
root@091606fc98de:/www/go_demo# cat main.go
package main
import "fmt"
func main() {
var a string = "Hello World"
fmt.Println(a)
}root@091606fc98de:/www/go_demo# go run .
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
# bash
root@091606fc98de:/www/go_demo# go version
go version go1.17.5 linux/amd64
root@091606fc98de:/www/go_demo# ls -al
total 4
drwxrwxrwx 1 root root 4096 Oct 23 09:46 .
drwxr-xr-x 3 root root 4096 Oct 23 09:38 ..
-rwxrwxrwx 1 root root 96 Oct 23 09:46 main.go
root@091606fc98de:/www/go_demo# vim main.go
root@091606fc98de:/www/go_demo# cat main.go
package main
import "fmt"
func main() {
var a string = "Hello World"
fmt.Println(a)
}root@091606fc98de:/www/go_demo# go run .
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
root@091606fc98de:/www/go_demo# export GO111MODULE=auto
root@091606fc98de:/www/go_demo# go run .
Hello World
root@091606fc98de:/www/go_demo#
如果强制启用Go Modules机制,环境变量中如果设置GO111MODULE=on,则需要先初始化模块:go mod init hello
未初始化否则会报错误:go: go.mod file not found in current directory or any parent directory; see 'go help modules'
修改成自动避免报错误
export GO111MODULE=auto
转载注明:
感谢博主,喝杯咖啡~
感谢博主,喝杯咖啡~
还没有人发表评论