奇怪的golang包导入问题 [英] Strange golang package import issue

查看:222
本文介绍了奇怪的golang包导入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是目录树:

  + /项目
+ --- / bin
+ --- / pkg
+ --- / src
+ --- / client_test
--- --- client_test.go
--- --- main.go

在main.go中:

  package main 
import(client_test)
func main(){
client_test.Send()
}

在client_test.go中:

  package client_test 
func Send(){
}

错误:

  src / main.go | 8 |导入并未使用:client_test
src / main.go | 32 | undefined:client_test

我读过如何在golang中使用自定义软件包?,我想我已经有了像这个人一样的解决方案,但是我只是没有知道如何解决这个问题。

go env:

  GOARCH =amd64 
GOBIN =/ usr / local / go / bin
GOCHAR =6
GOEXE =
GOGCCFLAGS = - g -O2 -fPIC -m64 -pthread -fno-common
GOHOSTARCH =amd64
GOHOSTOS =darwin
GOOS =darwin
GOPATH =/ Users / staff / projects / Minigame_Server正好是我的工作目录)
GOROOT =/ usr / local / go
GOTOOLDIR =/ usr / local / go / pkg / tool / darwin_amd64
CGO_ENABLED =1


解决方案


Command go,Test packages。



...名称匹配的文件文件模式* _test.go...
可以包含测试函数,基准函数和示例函数。

不要使用保留的名称。例如,在整个过程中用 clienttest 替换 client_test


Here is the directory tree:

+/project  
  +---/bin  
  +---/pkg  
  +---/src  
    +---/client_test  
      +---client_test.go  
    +---main.go  

In main.go:

package main
import ("client_test")
func main() {
  client_test.Send()
}

In client_test.go:

package client_test
func Send() {
}

Error:

src/main.go|8| imported and not used: "client_test"
src/main.go|32| undefined: client_test

I've read How to use custom packages in golang? and I think I've had the same solution like this guy, but I just don't know how to solve this problem. Please help.

go env:

GOARCH="amd64"  
GOBIN="/usr/local/go/bin"  
GOCHAR="6"  
GOEXE=""  
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"  
GOHOSTARCH="amd64"  
GOHOSTOS="darwin"  
GOOS="darwin"  
GOPATH="/Users/staff/projects/Minigame_Server" (that's exactly my working directory)  
GOROOT="/usr/local/go"  
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"  
CGO_ENABLED="1"  

解决方案

Command go, Test packages.

... files with names matching the file pattern "*_test.go" ... can contain test functions, benchmark functions, and example functions.

Don't use reserved names. For example, replace client_test with clienttest throughout.

这篇关于奇怪的golang包导入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆