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

Resolving the Go Error: 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

Error in Go: package_mytest.go:5:2: cannot find package "." in: E:\wwwroot\go\the-way-to-go\package\pack1.
Figure 1
Plaintext

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. View the code of pack1.go

Go

package pack1
var Pack1Int int = 42
var pack1Float = 3.14

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

3. View the code of package_mytest.go

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. View environment variables – Administrator’s user variables – GOPATH, its value is: C:\Users\Administrator\go, as shown in Figure 2

View environment variables - Administrator's user variables - GOPATH, its value is: 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 locally via the command: go install pack1, this will copy pack1.a to C:\Users\Administrator\go\pkg\windows_amd64. As shown in Figure 3

Compile and install a package. Copy pcak1.go to C:\Users\Administrator\go\src\pack1\pack1.go. Compile and install the package locally via the command: go install pack1, this will copy pack1.a to C:\Users\Administrator\go\pkg\windows_amd64.
Figure 3
Plaintext
PS C:\Users\Administrator\go> cd .\src\pack1\
PS C:\Users\Administrator\go\src\pack1> ls


    Directory: 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


    Directory: 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, executed successfully, changing “./pack1” to “pack1” also executes successfully. As shown in Figure 4

Run: go run .\package_mytest.go, executed successfully
Figure 4
Plaintext

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  ·  邮件联系