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

Fixing the “cannot find package” Error When Running Go

运行文件,Go 报错:exercise-maps.go:4:2: cannot find package "code.google.com/p/go-tour/wc" in any of: c:\go\src\code.google.com\p\go-tour\wc (from $GOROOT) C:\Users\Administrator\go\src\code.google.com\p\go-tour\wc (from $GOPATH)

1. View the Go file: exercise-maps.go, the code is as follows

Go
package main

import (
	"code.google.com/p/go-tour/wc"
)

func WordCount(s string) map[string]int {
	return map[string]int{"x": 1}
}

func main() {
	wc.Test(WordCount)
}

2. Run the file, Go reports an error: exercise-maps.go:4:2: cannot find package “code.google.com/p/go-tour/wc” in any of:
c:\go\src\code.google.com\p\go-tour\wc (from $GOROOT)
C:\Users\Administrator\go\src\code.google.com\p\go-tour\wc (from $GOPATH), as shown in Figure 1

Run the file, Go reports an error: exercise-maps.go:4:2: cannot find package "code.google.com/p/go-tour/wc" in any of: c:\go\src\code.google.com\p\go-tour\wc (from $GOROOT) C:\Users\Administrator\go\src\code.google.com\p\go-tour\wc (from $GOPATH)
Figure 1
Plaintext
PS E:\wwwroot\go\moretypes> go run exercise-maps.go
exercise-maps.go:4:2: cannot find package "code.google.com/p/go-tour/wc" in any of:
        c:\go\src\code.google.com\p\go-tour\wc (from $GOROOT)
        C:\Users\Administrator\go\src\code.google.com\p\go-tour\wc (from $GOPATH)

3. Open the Go Tour GitHub URL: https://github.com/golang/tour . After enabling the Lantern connection, install Go Tour from source. It reports an error: unrecognized import path “golang.org/x/tour”. Pinging golang.org prompts request timed out, as shown in Figure 2

Open the Go Tour GitHub URL: https://github.com/golang/tour . After enabling the Lantern connection, install Go Tour from source. It reports an error: unrecognized import path "golang.org/x/tour". Pinging golang.org prompts request timed out
Figure 2
Plaintext
PS E:\wwwroot\go\moretypes> go get golang.org/x/tour
package golang.org/x/tour: unrecognized import path "golang.org/x/tour" (https fetch: Get https://golang.org/x/tour?go-g
et=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly res
pond after a period of time, or established connection failed because connected host has failed to respond.)

4. Open the URL https://golang.org/ in Chrome, and it loads normally, as shown in Figure 3

Open the URL https://golang.org/ in Chrome, and it loads normally
Figure 3

5. Use Lantern to configure an HTTP proxy for the command line. View Lantern’s advanced settings – HTTP(S) proxy server, as shown in Figure 4

Use Lantern to configure an HTTP proxy for the command line. View Lantern's advanced settings - HTTP(S) proxy server
Figure 4

6. Set the two environment variables HTTP_PROXY and HTTPS_PROXY in cmd. Execute the following two commands to set the environment variables and complete the configuration. Installing Go Tour from source no longer reports an error. Although Ping still fails, you can use Curl to open www.google.com, which indicates that the HTTP proxy has been set up successfully, as shown in Figure 5

Set the two environment variables HTTP_PROXY and HTTPS_PROXY in cmd. Execute the following two commands to set the environment variables and complete the configuration. Installing Go Tour from source no longer reports an error. Although Ping still fails, you can use Curl to open www.google.com, which indicates that the HTTP proxy has been set up successfully
Figure 5
Plaintext
C:\Users\Administrator>set HTTP_PROXY=http://127.0.0.1:50999

C:\Users\Administrator>set HTTPS_PROXY=http://127.0.0.1:50999

C:\Users\Administrator>go get golang.org/x/tour

C:\Users\Administrator>ping golang.org

Pinging golang.org [216.239.37.1] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 216.239.37.1:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

C:\Users\Administrator>curl -vv http://www.google.com
* Rebuilt URL to: http://www.google.com/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 50999 (#0)
> GET http://www.google.com/ HTTP/1.1
> Host: www.google.com
> User-Agent: curl/7.55.1
> Accept: */*
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 301 Moved Permanently
< Connection: close
< Cache-Control: max-age:86400
< Date: Friday, 27-Dec-19 16:57:05 CST
< Expires: Sat, 28 Dec 2019 16:57:05 GMT
< Keep-Alive: timeout=58
< Location: https://www.google.com/
< Content-Length: 0
<
* Closing connection 0


7. View the directory: C:\Users\Administrator\go\src\golang.org\x, the directory: tour already exists, as shown in Figure 6

View the directory: C:\Users\Administrator\go\src\golang.org\x, the directory: tour already exists
Figure 6

8. Edit the Go file: exercise-maps.go, replace code.google.com/p/go-tour with: golang.org/x/tour, the code is as follows

Go
package main

import (
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	return map[string]int{"x": 1}
}

func main() {
	wc.Test(WordCount)
}

9. Run the file, it runs normally, as shown in Figure 7

Run the file, it runs normally
Figure 7
Plaintext
PS E:\wwwroot\go\moretypes> go run exercise-maps.go
FAIL
 f("I am learning Go!") =
  map[string]int{"x":1}
 want:
  map[string]int{"Go!":1, "I":1, "am":1, "learning":1}

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

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

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

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

关于我  ·  GitHub  ·  邮件联系