dongluzhi5208 2015-11-04 22:55
浏览 231
已采纳

导入的Golang软件包说未定义/不可用

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"
  • 写回答

2条回答 默认 最新

  • dongxi3911 2015-11-05 00:01
    关注

    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{ ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算