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

Error in Go: package_mytest.go:5:2: cannot find package “.” in: e:\wwwroot\go\the-way-to-go\package\pack1

在 Go 中报错:package_mytest.go:5:2: cannot find package "." in: E:\wwwroot\go\the-way-to-go\package\pack1。

1. Error in Go: package_mytest.go:5:2: cannot find package “.” in: E:\WWWRoot\Go\The-way-to-go\package\pack1. as shown in Figure 1

在 Go 中报错:package_mytest.go:5:2: cannot find package "." in: E:\wwwroot\go\the-way-to-go\package\pack1。
Figure 1
PS E:\wwwroot\go\the-way-to-go\package> go run .\package_mytest.go
package_mytest.go:5:2: cannot find package "." in:
        E:\wwwroot\go\the-way-to-go\package\pack1

2. Check the code of pack1.go

package pack1
var Pack1Int int = 42
var pack1Float = 3.14

func ReturnStr() string {
	return "Hello main!"
}

3. Check the code of package_mytest.go

package main

import (
	"fmt"
	"./pack1"
)


func main() {
	var test1 string
	test1 = pack1.ReturnStr()
	fmt.Printf("ReturnStr from package1: %s\n", test1)
	fmt.Printf("Integer from package1: %d\n", pack1.Pack1Int)
	// fmt.Printf("Float from package1: %f\n", pack1.pack1Float)
}

4. Check the environment variables – the user variables of the administrator – GOPATH, whose values are: C:\Users\Administrator\Go, as shown in Figure 2

查看环境变量 - Administrator 的用户变量 - GOPATH,其值为:C:\Users\Administrator\go
Figure 2

5. Compile and install a package. Copy pcak1.go to C:\Users\Administrator\Go\Src\Pack1\Pack1.go. Compile and install the package to the local: Go install Pack1, which will copy Pack1.a to C:\Users\Administrator\Go\Pkg\Windows_amd64. as shown in Figure 3

编译并安装一个包。复制 pcak1.go 至 C:\Users\Administrator\go\src\pack1\pack1.go。通过指令编译并安装包到本地:go install pack1, 这会将 pack1.a 复制到 C:\Users\Administrator\go\pkg\windows_amd64 下面。
Figure 3
PS C:\Users\Administrator\go> cd .\src\pack1\
PS C:\Users\Administrator\go\src\pack1> ls


    目录: C:\Users\Administrator\go\src\pack1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2020/06/23     19:23            114 pack1.go


PS C:\Users\Administrator\go\src\pack1> go install pack1
PS C:\Users\Administrator\go\src\pack1> cd ..
PS C:\Users\Administrator\go\src> cd ..
PS C:\Users\Administrator\go> cd .\pkg\windows_amd64\
PS C:\Users\Administrator\go\pkg\windows_amd64> ls


    目录: C:\Users\Administrator\go\pkg\windows_amd64


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2019/12/27     17:01                golang.org
-a----       2020/07/02     19:30           1216 pack1.a
-a----       2020/07/02     19:20           1592 uc.a



6. Run: go run .\package_mytest.go, the execution is successful, “./pack1” can also be executed successfully. as shown in Figure 4

运行:go run .\package_mytest.go,执行成功
Figure 4
PS E:\wwwroot\go\the-way-to-go\package> go run .\package_mytest.go
ReturnStr from package1: Hello main!
Integer from package1: 42

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

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

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

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

关于我  ·  GitHub  ·  邮件联系