英文:
Imported Golang Package Saying Undefined / Not Available
问题
所以我想创建一个库,我可以从正在构建的脚本/项目中使用。该库名为go_nessus(完整源代码:http://github.com/kkirsche/go-nessus),但是在导入时遇到了问题。
go_nessus代码示例:
package go_nessus
import (
"fmt"
)
func (nessus *Nessus) MakeClient(host, port, accessKey, secretKey string) Nessus {
return Nessus{
Ip: fmt.Sprintf("%s", host),
Port: fmt.Sprintf("%s", port),
AccessKey: fmt.Sprintf("%s", accessKey),
SecretKey: fmt.Sprintf("%s", secretKey),
}
}
然而,当我尝试使用它时,出现以下错误:
~/g/G/s/g/k/attempt ❯❯❯ go install -race && $GOPATH/bin/attempt
# github.com/kkirsche/attempt
./attempt.go:6: undefined: go_nessus.MakeClient
测试文件如下:
package main
import "github.com/kkirsche/go-nessus"
func main() {
nessusClient := go_nessus.MakeClient("localhost", "8834", "ExampleAccessKey", "ExampleSecretKey")
}
不幸的是,我无法弄清楚如何在没有错误的情况下实际使用我创建的方法。非常感谢您帮助我找出导入过程中出了什么问题。
我的go env
:
GOARCH="amd64"
GOBIN="/Users/kkirsche/git/Go/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/kkirsche/git/Go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT=""
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
英文:
So I want to create a library that I can use from a script / project I'm building. The library is called go_nessus, (full source code: http://github.com/kkirsche/go-nessus) but I'm having an issue when importing it.
An example of the go_nessus code:
# go-nessus/client
package go_nessus
import (
"fmt"
)
func (nessus *Nessus) MakeClient(host, port, accessKey, secretKey string) Nessus {
return Nessus{
Ip: fmt.Sprintf("%s", host),
Port: fmt.Sprintf("%s", port),
AccessKey: fmt.Sprintf("%s", accessKey),
SecretKey: fmt.Sprintf("%s", secretKey),
}
}
When attempting to use this though I get the following error:
~/g/G/s/g/k/attempt ❯❯❯ go install -race && $GOPATH/bin/attempt
# github.com/kkirsche/attempt
./attempt.go:6: undefined: go_nessus.MakeClient
The test file looks like so:
package main
import "github.com/kkirsche/go-nessus"
func main() {
nessusClient := go_nessus.MakeClient("localhost", "8834", "ExampleAccessKey", "ExampleSecretKey")
}
I sadly though can't figure out how to actually use the methods I've created without errors. Any help would be greatly appreciated in figuring out what's wrong with my import process.
My go env
:
GOARCH="amd64"
GOBIN="/Users/kkirsche/git/Go/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/kkirsche/git/Go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT=""
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
答案1
得分: 1
问题出在这里:
func (nessus *Nessus) MakeClient(host, port, accessKey, secretKey string) Nessus {
以及这里:
nessusClient := go_nessus.MakeClient("localhost", "8834", "ExampleAccessKey", "ExampleSecretKey")
在你的包中,你将函数作为Nessus
类型的成员函数进行了定义,
但是在主函数中,你却像调用包级函数一样调用它。
我认为你想要将MakeClient
定义为:
func MakeClient(host, port, accessKey, secretKey string) Nessus {
这样它就成为了包级函数。
另外,如果你不想一直复制结构体,你可能想在这里使用指针:
func MakeClient(host, port, accessKey, secretKey string) *Nessus {
然后:
return &Nessus{ ...
英文:
the problem lies here:
func (nessus *Nessus) MakeClient(host, port, accessKey, secretKey string) Nessus {
and here:
nessusClient := go_nessus.MakeClient("localhost", "8834", "ExampleAccessKey", "ExampleSecretKey")
in your package, you are making the function a member of type Nessus
but in main you call it as though it were a package level function
I believe you want MakeCliet
to be defined as:
func MakeClient(host, port, accessKey, secretKey string) Nessus {
which makes it a package level function.
Also, you may want to use pointers here if you don't want to copy the struct around all the time, which would be:
func MakeClient(host, port, accessKey, secretKey string) *Nessus {
and then:
return &Nessus{ ...
答案2
得分: 0
似乎这是一个指针问题。代码应该是这样的:
package gonessus
import (
"fmt"
)
func (nessus Nessus) MakeClient(host, port, accessKey, secretKey string) *Nessus {
return &Nessus{
Ip: fmt.Sprintf("%s", host),
Port: fmt.Sprintf("%s", port),
AccessKey: fmt.Sprintf("%s", accessKey),
SecretKey: fmt.Sprintf("%s", secretKey),
}
}
英文:
It seems this is a pointers issue. The code should be:
package gonessus
import (
"fmt"
)
func (nessus Nessus) MakeClient(host, port, accessKey, secretKey string) *Nessus {
return &Nessus{
Ip: fmt.Sprintf("%s", host),
Port: fmt.Sprintf("%s", port),
AccessKey: fmt.Sprintf("%s", accessKey),
SecretKey: fmt.Sprintf("%s", secretKey),
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论