回答

收藏

Go:非本地包中的本地导入

技术问答 技术问答 282 人阅读 | 0 人回复 | 2023-09-11

我有以下文件结构:, r4 B4 Q2 ^) y0 L- Y# P
    .├── bin│   └── hello├── pkg└── src    └── jacob.uk.com        ├── greeting        │   └── greeting.go        └── helloworld.go5 directories,3 files
    ( z5 F+ {' w1 J/ ]* s7 ]: r
使用以下 GOPATH
+ H8 [7 P- }1 k# Y+ C
    /Users/clarkj84/Desktop/LearningGo
      o8 y  j2 Y5 a. B8 ~2 R
在文件夹/usr/local/go/bin/go install jacob.uk.com内执行后src,我收到错误local import "./greeting" in non-local package( ^, U7 n+ T) \' Y/ i
helloworld.go:) N7 M# V; C$ z6 T* I

    # |% A) a2 Z' F6 P/ j; C& |
  • package main;import "./greeting"func main()code]               
    / m6 I/ k' N! b: s3 M) C! \2 H& M
  •     解决方案:                                                               ' ^* K) n- K% B. }  q6 S# D0 z8 s
  •                                                                 将非本地包指定为go install. 如果想在当地引进工作,首先要把工作目录改成src/jacob.uk.com然后执行go install(不指定包)。
    & `$ ^0 s) M0 ~$ O7 `- Z- y* V
  • 当然,具有helloworld.go你会得到一个编译错误:imported and not used。但是一旦你使用了导入greeting它应该能够编译包中的东西。
    + ~# X5 [- z( K( j
  • 但是你根本不应该使用本地导入。而是写:[code]import "jacob.uk.com/greeting"
    : ]" ~& x; x7 g+ ~* @
您将能够从任何地方编译/操作/安装它。
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则