无效的导入路径 – Go + Windows

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

Invalid import path - Go + windows

问题

尝试从Windows文件系统导入文件时,遇到了以下错误:

server\main.go:10:5: invalid import path: "github.com\\sewelol\\sgx-decryption-service\\decryptionservice"

我已经检查了$PATH环境变量,确保其中包含了包含github.com的目录,并且$GOROOT也已经设置为指向Go的安装路径。

我猜测问题可能与文件路径本身有关,但是我找不到关于如何在Windows环境中处理文件路径的任何信息。

谢谢。

英文:

When attempting to import to files from the windows file system:

import (
    pb "github.com\\sewelol\\sgx-decryption-service\\decryptionservice"
    dev "github.com\\sewelol\\sgx-decryption-service\\device"
	"google.golang.org\\rpc"`
)

I get this error

server\main.go:10:5: invalid import path: "github.com\\sewelol\\sgx-decryption-service\\decryptionservice"

I have checked $PATH enviroment variable includes the directory that has github.com, and $GOROOT also is set up to point to the Go installation.

I assume it is something to do with the file paths themselves, but I can't find any information on how to do filepaths in the windows environment.

Thanks

答案1

得分: 4

你必须在导入路径(导入声明中)使用正斜杠/,即使你在Windows上。

规范:导入声明:

实现限制:编译器可以限制ImportPaths为非空字符串,仅使用属于Unicode的 L、M、N、P和S通用类别的字符(没有空格的图形字符),还可以排除字符!"#$%&'()*,:;<>?[\]^{|}和Unicode替换字符U+FFFD

任何编译器都可以排除反斜杠\字符等其他字符。即使你使用的编译器不排除反斜杠字符,你的代码也不具备可移植性。

所以,你可以尝试这样写:

import (
    pb "github.com/sewelol/sgx-decryption-service/decryptionservice"
    dev "github.com/sewelol/sgx-decryption-service/device"
    "google.golang.org/rpc"
)
英文:

You have to use forward slashes / in import paths (of import declarations), even if you're on Windows.

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 !&quot;#$%&amp;&#39;()*,:;&lt;=&gt;?[\]^{|}` and the Unicode replacement character U+FFFD.

Any compiler may exclude the backslash \ character among others. Even if you would use one that doesn't, your code would not be portable.

So instead try:

import (
    pb &quot;github.com/sewelol/sgx-decryption-service/decryptionservice&quot;
    dev &quot;github.com/sewelol/sgx-decryption-service/device&quot;
    &quot;google.golang.org/rpc&quot;
)

huangapple
  • 本文由 发表于 2017年7月26日 18:05:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/45323772.html
匿名

发表评论

匿名网友

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

确定