Golang在从“main”文件夹导入时出现无效的导入路径。

huangapple go评论72阅读模式
英文:

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 (
    &quot;atom-first-project/main/Extension&quot;
)

还要注意,包名(通常是文件夹名,但不一定)必须是有效的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 (
    &quot;atom-first-project/main/Extension&quot;
)

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 .

huangapple
  • 本文由 发表于 2017年9月8日 18:37:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/46114556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定