Skip to content

调用外部包中的代码

Updated View as Markdown

引言

使用

以 GoLang 教程中的 quote 为例子:

打开 hello.go

粘贴代码:

package main

import "fmt"

import "rsc.io/quote"

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

设置中国大陆代理(可选)

# 1. 七牛 CDN
go env -w  GOPROXY=https://goproxy.cn,direct
# 2. 阿里云
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct

处理导入的模块到 go.mod

运行:

go mod tidy

输出:

$ go mod tidy
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2

此为导入包模块到 go.mod.

运行你的代码,查看你调用的函数生成的消息。

$ go run .
Don't communicate by sharing memory, share memory by communicating.

在此函数中

此处导入 quote 函数,在 fmt.Println() 方法中使用 quote.Go() 函数。

由此fmt.Println() 方法可以嵌套使用函数。

参考

https://golang.google.cn/doc/tutorial/getting-started

Navigation

Type to search…

↑↓ navigate↵ selectEsc close