GO TOUR slicing exercise to run the full record of the pit on the pit (from error report to successful operation)
Recently, when I was doing the slices practice of the official go to the go, I encountered a series of dependencies download and running errors, and after tossing, I finally succeeded in running through. I have sorted out the complete stepping process and solutions, hoping to help those who practice Go Tour locally like me, and avoid repeated detours.
Let’s briefly introduce the background: I use the WSL + Ubuntu + Docker development environment, with VS code, the Go module has been initialized locally, and the practice directory structure is /app/go-tour/ (the root directory has go.mod), the slice practice code is placed in /app/go-tour/exercies-slices/main.go, the goal is to run the code to generate pictures and complete the implementation of the pic function.
1. Practice goals and initial code
The core requirement of this exercise: Implement the PIC function and return a slice of length dy, each element is an 8-bit unsigned integer slice of length dx ([][]uint8), after running the program, these integers will be parsed into grayscale (blue level) values, displaying the corresponding picture.
Initial runnable code (and finally successfully run version):
package main
import "golang.org/x/tour/pic"
// Pic 函数:生成 dx * dy 的图片切片
func Pic(dx, dy int) [][]uint8 {
// 创建外层切片,长度 dy
image := make([][]uint8, dy)
// 遍历每一行
for y := range image {
// 创建每一行的切片,长度 dx
row := make([]uint8, dx)
// 遍历每一列
for x := range row {
// 你可以任选下面一种公式,效果不同
// row[x] = uint8((x + y) / 2) // 渐变
// row[x] = uint8(x * y) // 格子状
row[x] = uint8(x ^ y) // 斜纹(最漂亮)
}
image[y] = row
}
return image
}
func main() {
pic.Show(Pic)
}
2. The complete process of stepping on the pit (error report → investigation → solution)
- First run: Missing dependencies are missing
Enter the exercise directory and run the code directly, the first thing you encounter is the dependency missing error: as shown in Figure 1


/app/go-tour/exercies-slices # go run main.go
main.go:3:8: no required module provides package golang.org/x/tour/pic; to add it:
go get golang.org/x/tour/pic
The reason is obvious: the golang.org/x/tour/pic package is imported in the code, but this dependency is not installed locally, Go prompts us to install through the go get command.
- Installation dependencies: 502 bad gateway error
Follow the prompts, switch to the go-tour root directory (because the root directory has go.mod, you can manage the dependencies uniformly), and execute the installation command, but you encounter 502 errors: as shown in Figure 2
/app/go-tour # go get golang.org/x/tour/pic go: golang.org/x/tour/pic: module golang.org/x/tour: reading https://goproxy.io/golang.org/x/tour/@v/list: 502 Bad Gateway server response: error code: 502
I was a little confused at first, because I can already go over the wall locally, and Google can also access it normally, why do I still report 502? Later, the investigation found that it was because GoProxy was set in the system environment variable (goProxy.io in the domestic), and golang.org/x/tour is go For the exclusive package of the official tutorial, most domestic public agents do not cache this package, resulting in the failure of the proxy access and return 502.
- Try modifying GoProxy: can’t override system environment variables
Since the domestic agent is not good, I want to change the goproxy to direct (direct access to the official source), and execute the following command:
/app/go-tour # go env -w GOPROXY=direct warning: go env -w GOPROXY=… does not override conflicting OS environment variable
A new warning has appeared: go env -w cannot override the set GoProxy in the system environment variable. This means that GoProxy is forced at the system level, which cannot be modified through go env -w, and GoProxy must be canceled in the system environment variable first.
- Cancel GoProxy: Successfully installed dependencies
Since it cannot be modified, then temporarily cancel the GoProxy in the system environment variable, and let Go directly go over the network to access the official source, and execute the following command:
/app/go-tour # unset GOPROXY /app/go-tour # go get golang.org/x/tour/pic go: downloading golang.org/x/tour v0.1.0 go: added golang.org/x/tour v0.1.0
This time it’s finally successful! You can see that Go successfully downloaded the golang.org/x/tour v0.1.0 version and added it to the project dependencies (updated to go.mod and go.sum).
- Run again: successfully generate the picture
After the dependency installation is complete, switch to the exercise directory running code again:
/app/go-tour # cd exercies-slices/
/app/go-tour/exercies-slices # go run main.go
IMAGE:iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAAEEElEQVR42uzdQYrrMBCE4ZbJxXW0PkqOkGUWwcPADMwsvPBCxlJ/YH6KQhjzYJ5SqbT1iNhbC5er5rW1FvseiDX5+P4j2OLz+f2DoOlK2g6AdoDt58/i/f7/8YjPX923A6AdwGdBWgZArJ4B/l6v1/FXp9Zbv8p6OwDaAXwWpGUARD2A74n5egBEGYCmZQDEqj3A0fV8nv/Jtfu7/13vbwdAO4DPgrQMgKgH8D0xXw+AKAPQtAyAaB7A78utNw+AKAPQtAyAqAfg8/UAiDIATcsAiFXmAc5emTH8Fe+e3/M7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oGkZAPEafgUAAP//2P/knioA0wcAAAAASUVORK5CYII=
If you see the base64 code that starts with the output of a series of images, it means that the operation is successful! This code is the binary data of the picture, copy it directly to the browser address bar (or use the online base64 to picture tool), you can see the twill pictures we generated with x^y.
3. Summary of key knowledge points and solutions
- The core error is sorted out
- Error 1 (missing dependencies): No required module provides package golang.org/x/tour/pic → The corresponding dependency package is not installed, you need to install it with go get.
- Error 2 (502 Bad Gateway): domestic GoProxy does not cache golang.org/x/tour package, causing the download to fail.
- Warning (cannot modify goProxy): GoProxy has been set in the system environment variable, go env -w cannot be overwritten, it needs to be temporarily canceled.
- Universal solution (for all Go Tour exercises)
If you can also go over the wall and encounter similar dependency download problems, just follow the following steps: - Switch to the Go Tour project root directory (make sure that the root directory has a go.mod file): cd /app/go-tour
- Temporarily cancel goProxy in system environment variable: unset goproxy (only the current terminal is active)
- Dependencies required for installation: go get golang.org/x/tour/pic (other exercise dependencies can also be installed in this way)
- Switch to the exercise directory, run the code: go run main.go
- Supplement
- About GoProxy: If there is no overturn, you can switch to a proxy available in the country (such as go env -w goproxy=https://goproxy.cn, direct), but some official packages (such as Tour) may still not be able to be downloaded, it is recommended to use the method of circumventing the wall first + cancel goProxy.
- Regarding dependencies sharing: After installing a dependency once in the go-tour root directory, all subdirectories exercises (such as exercies-slices, exercies-maps, etc.) can share the dependency without repeated installation.
- Picture view: The base64 encoding output after running, can be copied to the base64 picture tool, and the picture is generated with one click to view the effect.
4. Summary
This step on the pit has given me a deep understanding of the dependency management logic of Go Modules, as well as the role and usage scenarios of the GoProxy proxy. In fact, the core of the problem is very simple: when you can climb the wall, you don’t need to rely on domestic agents, cancel GoProxy and let Go directly access the official source, and you can download all the official dependencies smoothly.
For Go beginners, local practice of official Tour is a good way to consolidate the foundation, but don’t panic when encountering dependence problems, check step by step, focus on GoProxy and dependency installation path, you can solve it quickly. In the future, I will continue to sort out the records of stepping on the pit in the process of Go learning. Welcome everyone to exchange and learn together~
Finally, attach my exercise code repository: github.com/shuijingwan/go-gin-learning, which includes all the locally runable versions of the exercises of Go Tour. If you are interested, you can refer to it.