goproxy.io
您只需通过简单设置
Bash (Linux or macOS)
# 配置 GOPROXY 环境变量
export GOPROXY=https://proxy.golang.com.cn,direct
# 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔(可选)
export GOPRIVATE=git.mycompany.com,github.com/my/private
PowerShell (Windows)
# 配置 GOPROXY 环境变量
$env:GOPROXY = "https://proxy.golang.com.cn,direct"
# 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔(可选)
$env:GOPRIVATE = "git.mycompany.com,github.com/my/private"
https://goproxy.io/zh/
Get started with Hello, World.
- Open a command prompt and cd to your home directory.
On Linux or Mac:
cd
On Windows:
cd %HOMEPATH%
- Create a hello directory for your first Go source code.
For example, use the following commands:
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.For the purposes of this tutorial, just use
example/hello
.$ 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.
package main import "fmt" func main() { fmt.Println("Hello, World!") }
This is your Go code. In this code, you:
- Declare a
main
package (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. Amain
function executes by default when you run themain
package.
- Declare a
-
Run your code to see the greeting.
$ go run . Hello, World!
The
go run
command is one of manygo
commands you'll use to get things done with Go. Use the following command to get a list of the others:$ go help
Call code in an external package
When you need your code to do something that might have been implemented by someone else, you can look for a package that has functions you can use in your code.
- Make your printed message a little more interesting with a function from an external module.
- Visit pkg.go.dev and search for a "quote" package.
- Locate and click the
rsc.io/quote
package in search results (if you seersc.io/quote/v3
, ignore it for now). - In the Documentation section, under Index, note the list of functions you can call from your code. You'll use the
Go
function. - At the top of this page, note that package
quote
is included in thersc.io/quote
module.
You can use the pkg.go.dev site to find published modules whose packages have functions you can use in your own code. Packages are published in modules -- like
rsc.io/quote
-- where others can use them. Modules are improved with new versions over time, and you can upgrade your code to use the improved versions. - In your Go code, import the
rsc.io/quote
package and add a call to itsGo
function.After adding the highlighted lines, your code should include the following:
package main import "fmt" import "rsc.io/quote" func main() { fmt.Println(quote.Go()) }
- Add new module requirements and sums.
Go will add the
quote
module as a requirement, as well as a go.sum file for use in authenticating the module. For more, see Authenticating modules in the Go Modules Reference.$ go mod tidy go: finding module for package rsc.io/quote go: found rsc.io/quote in rsc.io/quote v1.5.2
- Run your code to see the message generated by the function you're calling.
$ go run . Don't communicate by sharing memory, share memory by communicating.
Notice that your code calls the
Go
function, printing a clever message about communication.When you ran
go mod tidy
, it located and downloaded thersc.io/quote
module that contains the package you imported. By default, it downloaded the latest version -- v1.5.2.
https://golang.google.cn/doc/tutorial/getting-started
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律