英文:
Golang invalid import path when importing from "main" folder
问题
最近几天一直在尝试解决安装main.go文件时出现的"invalid import path: Atom First project/main/Extension (build)"错误,但是我还没有找到错误的原因。
操作系统 - Windows 10
IDE - Atom
GOBIN - E:\Github Repository\Programming\Golang\bin
GOPATH - E:\Github Repository\Programming\Golang
文件目录 - E:\Github Repository\Programming\Golang\src\Atom First project\main\main.go
E:\Github Repository\Programming\Golang\src\Atom First project\main\Extension/foo.go
main.go
package main
import (
"Atom First project/main/Extension"
)
func main() {
Extension.Extend()
}
foo.go
package Extension
import (
"fmt"
)
func Extend(){
fmt.Println("Hello from Extend func")
}
英文:
Been trying for the last few days to get rid of "invalid import path:"Atom First project/main/Extension" (build)" error when installing my main.go file but i haven't been able to find the reason behind the error.
OS - Windows 10
IDE - Atom
GOBIN - E:\Github Repository\Programming\Golang\bin
GOPATH - E:\Github Repository\Programming\Golang
File DIR- E:\Github Repository\Programming\Golang\src\Atom First project\main\main.go
E:\Github Repository\Programming\Golang\src\Atom First project\main\Extension/foo.go
main.go
package main
import (
"Atom First project/main/Extension"
)
func main() {
Extension.Extend()
}
foo.go
package Extension
import (
"fmt"
)
func Extend(){
fmt.Println("Hello from Extend func")
}
答案1
得分: 3
很简单:导入路径不能包含空格。规范:导入声明:
> 实现限制:编译器可以限制导入路径为非空字符串,只使用属于Unicode的L、M、N、P和S通用类别的字符(没有空格的图形字符),还可以排除字符!"#$%&amp;'()*,:; < = >?[\] ^`{|}和Unicode替换字符U+FFFD。
只需将Atom First project
文件夹重命名为例如atom-first-project
,然后更改导入声明。
import (
"atom-first-project/main/Extension"
)
还要注意,包名(通常是文件夹名,但不一定)必须是有效的Go 标识符。规范:包子句:
> 每个源文件都以包子句开头,并定义文件所属的包。
>
> PackageClause = "package" PackageName .
> PackageName = identifier .
英文:
It's simple: import paths cannot contain spaces. Spec: Import declarations:
> Implementation restriction: A compiler may restrict ImportPaths to non-empty strings using only characters belonging to Unicode's L, M, N, P, and S general categories (the Graphic characters without spaces) and may also exclude the characters !"#$%&'()*,:;<=>?[]^`{|} and the Unicode replacement character U+FFFD.
Simply rename your Atom First project
folder to e.g. atom-first-project
, and change the import declaration.
import (
"atom-first-project/main/Extension"
)
Also note that the package name (which is usually the folder name but not necessarily) must be a valid Go identifier. Spec: Package clause:
> A package clause begins each source file and defines the package to which the file belongs.
>
> PackageClause = "package" PackageName .
> PackageName = identifier .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论