Ubuntu 18.04下的Go语言学习笔记(一)Go的下载安装与模块调用

最近一门课程实验需要用到Go语言,久闻其名,为了减少以后配置的坑,特地记了一些笔记。不定期更新。

1. Go的下载与安装

下载页选择合适的版本下载。这里我选择的是go1.15.6.linux-amd64.tar.gz稳定版。

  1. 在root权限下解压

    sudo tar -C /usr/local -xzf go1.15.6.linux-amd64.tar.gz
    
  2. /usr/local/go/bin添加到环境变量中。增加到$HOME/.profile或者 /etc/profile (系统全局)。对于使用zsh等非bash环境的用户,可以修改.zshrc,并source

    export PATH=$PATH:/usr/local/go/bin
    
  3. 若安装成功,如下命令可以正常执行

    $ go version
    go version go1.15.6 linux/amd64
    

2. Go语言初探

用于用惯了IDEA与PyCharm,因此我也选择了Jetbrains家的Goland IDE。直接使用Jetbrains Toolbox可以方便下载。这是收费的,学生党可以用学校的邮箱去申请。没有licence的话可以使用免费的VSCode来开发。

2.1 Hello World

在目录下新建hello文件在,并创建hello.go文件

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

在命令行输入如下命令即可运行。

go run hello.go

2.2 调用外部包的代码

在pkg.go.dev搜索quote包, 并看其文档内容。之后将其加入到代码中

package main

import "fmt"

import "rsc.io/quote"

func main() {
    fmt.Println(quote.Go())
}

引用外部包后,需要有一个go.mod文件列出包的版本信息

$ go mod init hello
go: creating new go.mod: module hello

之后运行代码即可。gorun会自动定位包并下载默认的最新版本。

$ go: finding module for package rsc.io/quote
hello.go:4:8: 
module rsc.io/quote: Get "https://proxy.golang.org/rsc.io/quote/@v/list": dial tcp 172.217.24.17:443: i/o timeout

显然,在国内访问被墙了。考虑更换代理

go env -w GOPROXY=https://goproxy.cn

然后发现

$ go run hello.go 
go: finding module for package rsc.io/quote
go: writing stat cache: mkdir /usr/local/go/bin/pkg: permission denied
go: downloading rsc.io/quote v1.5.2
hello.go:4:8: mkdir /usr/local/go/bin/pkg: permission denied

没想到还有权限问题。

sudo chmod -R 777 /usr/local/go

这下终于成功了

$ go run hello.go
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
Don't communicate by sharing memory, share memory by communicating.

3. 创建 Go 模块

创建greetings文件夹,并运行

$ go mod init example.com/greetings
go: creating new go.mod: module example.com/greetings

可以看到生成了一个go.mod,内容为

module example.com/greetings

go 1.15

greetings.go代码

package greetings

import "fmt"

// Hello returns a greeting for the named person.
func Hello(name string) string {
    // Return a greeting that embeds the name in a message.
    message := fmt.Sprintf("Hi, %v. Welcome!", name)
    return message
}

Go语言中,:=是在一行中声明并初始化变量的简写,通过符号右边来指定变量的值。等价于

var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)

4.从另一个模块中调用模块

类似 2.Go 语言初探 中,在greetings的同级目录下建立hello目录,写hello.go

package main

import (
    "fmt"

    "example.com/greetings"
)

func main() {
    // Get a greeting message and print it.
    message := greetings.Hello("Gladys")
    fmt.Println(message)
}

并创建一个新模块

$ go mod init hello                
go: creating new go.mod: module hello

之后编辑hello模块来使用未发布的greetings模块

  1. 修改hello目录下的gomod

    module hello
    
    go 1.15
    
    replace example.com/greetings => ../greetings
    
  2. 运行

    $ go build         
    go: found example.com/greetings in example.com/greetings v0.0.0-00010101000000-000000000000
    
  3. 注意到go.mod中多了一行。这被称为 require directive

    module hello
    
    go 1.15
    
    replace example.com/greetings => ../greetings
    
    require example.com/greetings v0.0.0-00010101000000-000000000000
    
  4. 成功构建hello,运行择优

    ./hello       
    Hi, Gladys. Welcome!
    

参考链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值