没有不值得去解决的问题,也没有不值得去学习的技术!

A Complete Record of Pitfalls When Running the Go Tour Slices Exercise Locally: From Error to Success

Navigated to the exercise directory and ran the code directly, only to first encounter a missing dependency error

Recently, while working on the Slices exercise from the official Go Tour, I encountered a series of dependency download and runtime errors. After some troubleshooting, I finally got it running successfully. I have documented the entire troubleshooting process and solutions, hoping to help others practicing Go Tour locally avoid making the same detours.

First, a brief background: I am using a WSL + Ubuntu + Docker development environment with VS Code. I have already initialized a Go module locally, and my practice directory structure is /app/go-tour/ (with go.mod in the root directory). The slice exercise code is located at /app/go-tour/exercies-slices/main.go, and the goal is to run the code to generate an image and complete the implementation of the Pic function.

1. Exercise Objective and Initial Code
The core requirement of this exercise is to implement the Pic function, which returns a slice of length dy, where each element is a slice of 8-bit unsigned integers of length dx ([][]uint8). When the program runs, it parses these integers into grayscale (blue scale) values and displays the corresponding image.
Initial runnable code (the final successfully running version):

Go
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. Complete Troubleshooting Process (Error → Investigation → Solution)

  1. First run: Missing dependency package error
    Navigated to the exercise directory and ran the code directly, only to first encounter a missing dependency error: see Figure 1.
Installing dependencies: 502 Bad Gateway error
Navigated to the exercise directory and ran the code directly, only to first encounter a missing dependency error
Plaintext
/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 code imports the golang.org/x/tour/pic package, but this dependency is not installed locally. Go prompts us to install it using the go get command.

  1. Installing dependencies: 502 Bad Gateway error
    Following the prompt, I switched to the go-tour root directory (because the root directory contains go.mod for unified dependency management) and executed the installation command, but encountered a 502 error: see Figure 2.
Plaintext

/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 initially confused here because I already have VPN access locally and can access Google normally, so why was I still getting a 502? Later investigation revealed that the system environment variable GOPROXY was set to a domestic proxy (goproxy.io), and golang.org/x/tour is an exclusive package for the official Go Tour. Most domestic public proxies do not cache this package, causing the proxy access to fail and return a 502.

  1. Attempting to modify GOPROXY: Cannot override system environment variable
    Since the domestic proxy didn’t work, I tried changing GOPROXY to direct (to access the official source directly) by executing the following command:
Plaintext

/app/go-tour # go env -w GOPROXY=direct
warning: go env -w GOPROXY=… does not override conflicting OS environment variable

A new warning appeared: go env -w cannot override the GOPROXY already set in the system environment variables. This indicates that GOPROXY is forcibly set at the system level and cannot be modified via go env -w; the GOPROXY system environment variable must be unset first.

  1. Unsetting GOPROXY: Successfully installed dependencies
    Since I couldn’t modify it, I temporarily unset the GOPROXY system environment variable to let Go access the official source directly over the VPN by executing the following commands:
Plaintext

/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 finally succeeded! You can see that Go successfully downloaded version v0.1.0 of golang.org/x/tour and added it to the project dependencies (updating go.mod and go.sum).

  1. Running again: Successfully generated image
    After the dependencies were installed, I switched back to the exercise directory to run the code:
Plaintext
/app/go-tour # cd exercies-slices/
/app/go-tour/exercies-slices # go run main.go 
IMAGE:iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAAEEElEQVR42uzdQYrrMBCE4ZbJxXW0PkqOkGUWwcPADMwsvPBCxlJ/YH6KQhjzYJ5SqbT1iNhbC5er5rW1FvseiDX5+P4j2OLz+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DICoB+Dz9QCIMgBNywCI5gGst948AKIMQNMyAKIegM/XAyDKADQtAyCWOB8gM6Z+v77nX/v57QBoB/BZkJYBEPUAvifm6wEQZQCalgEQzQP4fbn15gEQZQCalgEQ9QB8vh4AUQagaRkA0fkA4f307u98AEQZgKZlAEQ9AJ+vB0CUAWhaBkA0D2C99eYBEGUAmpYBEPUAfL4eAFEGoGkZAPEafgUAAP//2P/knioA0wcAAAAASUVORK5CYII=

Seeing a string of Base64 encoding starting with IMAGE means the run was successful! This encoding is the binary data of the image. By copying it directly into your browser’s address bar (or using an online Base64-to-image tool), you can view the diagonal stripe image we generated using x^y.

3. Summary of Key Knowledge Points and Solutions

  1. Core error causes breakdown
  • Error 1 (Missing dependency): no required module provides package golang.org/x/tour/pic → The corresponding dependency package is not installed and needs to be installed using go get.
  • Error 2 (502 Bad Gateway): The domestic GOPROXY has not cached the golang.org/x/tour package, causing the download to fail.
  • Warning (Cannot modify GOPROXY): GOPROXY is already set in the system environment variables, and go env -w cannot override it; it needs to be temporarily unset.
  1. General solution (applicable to all Go Tour exercises)
    If you also have VPN access and encounter similar dependency download issues, simply follow these steps:
  2. Switch to the Go Tour project root directory (ensure there is a go.mod file in the root directory): cd /app/go-tour
  3. Temporarily unset the GOPROXY system environment variable: unset GOPROXY (effective only for the current terminal)
  4. Install the required dependencies: go get golang.org/x/tour/pic (dependencies for other exercises can also be installed this way)
  5. Switch to the exercise directory and run the code: go run main.go
  6. Additional notes
  • Regarding GOPROXY: If you do not have VPN access, you can switch to an available domestic proxy (e.g., go env -w GOPROXY=https://goproxy.cn,direct), but some official packages (like tour) may still fail to download. It is recommended to prioritize using a VPN + unsetting GOPROXY.
  • Regarding dependency sharing: After installing dependencies once in the go-tour root directory, all exercises in subdirectories (e.g., exercies-slices, exercies-maps, etc.) can share these dependencies without needing to reinstall them.
  • Viewing the image: The Base64 encoding output after running can be copied into a Base64-to-image tool to instantly generate and view the image result.

4. Conclusion
This troubleshooting experience gave me a deeper understanding of Go modules’ dependency management logic, as well as the role and use cases of the GOPROXY proxy. The core of the problem is actually quite simple: if you have VPN access, you don’t need to rely on a domestic proxy. Unsetting GOPROXY to let Go access the official source directly allows you to smoothly download all official dependencies.
For Go beginners, practicing the official Tour locally is a great way to solidify the fundamentals. Don’t panic when you encounter dependency issues; investigate step by step, focusing on GOPROXY and the dependency installation path, and you will resolve them quickly. I will continue to document my troubleshooting experiences while learning Go, and everyone is welcome to discuss and learn together~
Finally, here is my practice code repository: github.com/shuijingwan/go-gin-learning, which contains the locally runnable versions of all Go Tour exercises for those interested to reference.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系

评论

一条对“A Complete Record of Pitfalls When Running the Go Tour Slices Exercise Locally: From Error to Success”的回复

  1. […] The first pit: depend on download failure (502 bad gateway), you can refer to:GO TOUR slicing exercise to run the full record of the pit on the pit (from error report to successf… as shown in Figure […]