go(golang)语言Hello World!

在docker容器中安装go开发环境

查看go语言版本命令

go version
091606fc98de 容器ID
root@091606fc98de:/www/go_demo# go version
go version go1.17.5 linux/amd64
root@091606fc98de:/www/go_demo

go_demo目录新建文件main.go

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

在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 .

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# go run .
go: go.mod file not found in current directory or any parent directory; see 'go help modules'

处理go.mod file not fount,输出Hello World

# 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
点赞(0) 打赏

Comment list 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部