douchiwan1503 2016-11-02 03:44
浏览 87
已采纳

golang:自定义包和“未定义”

I've read the doc on creating custom packages, etc but I can't seem to pin down what the problem is.

GOPATH=/Users/lrsmith/GoWorkSpace
|->bin
|->pkg
|->src
    |->github.com
       |->lrsmith
          |-> zaphod
              |-> zaphod.go

I've done a 'go get github.com/lrsmith/go-icinga2-api/iapi' and it dropped it into the same dir as 'zaphod' and created and .a file under pkg.

GOPATH=/Users/lrsmith/GoWorkSpace
|->bin/
|->pkg/
  |->..../iapi.a
|->src/
    |->github.com/
       |->lrsmith/
          |-> zaphod/
              |-> zaphod.go
          |-> go-icinga2-api/

zaphod.go is very simple right now

package main
import (
    "github.com/lrsmith/go-icinga2-api/iapi"
)
func main () {
  t := iapi.Config("zaphod","beeblebrox","http://localhost",true)
}

When I do a go build in the zaphod directory I get ./zaphod.go:11: undefined: iapi.Config

I've read through the docs, checked cases and tried different structures but I can't seem to get it to load the package and let me call iapi.Config. The iapi code works and if I build something in the go-icinga2-api directory it works fine and the test all pass.

I want to create a separate project/code base that imports the go-icinga2-api and uses it, but can't seem to get it work.

Thanks Len

Added info. The structure for go-icinga2-api is

go-icinga2-api
|-> iapi
     |-> client.go
     |-> client_test.go
     |-> host.go
      .......

client.go is

// Package iapi provides a client for interacting with an Icinga2        Server
package iapi

import (
    "bytes"
    "crypto/tls"
    "encoding/json"
    "fmt"
    "net/http"
)

// Server ... Use to be ClientConfig
type Server struct {
    Username           string
    Password           string
    BaseURL                string
    AllowUnverifiedSSL bool
    httpClient         *http.Client
}

// func Config ...
func (server *Server) Config(username, password, url string, allowUnverifiedSSL bool) (*Server, error) {

    // TODO : Add code to verify parameters
    return &Server{username, password, url, allowUnverifiedSSL, nil}, nil

}

I've tried with the .go files up one level, i.e. not nested underneath iapi/ to for the same results.

  • 写回答

2条回答 默认 最新

  • donglang7236 2016-11-02 03:57
    关注

    Updated Answer

    In client.go, it looks like you're trying to use Config as a constructor for Server structs. Since Config is defined with a receiver (func (server *Server)), it's a method of Server and can't be called directly. You're code should work if you remove (server *Server).

    It's idiomatic to name constructors New[type being returned], or New if the type is the same name as the package.

    From the 3rd paragraph of the Package Names section of Idiomatic Go:

    the function to make new instances of ring.Ring—which is the definition of a constructor in Go—would normally be called NewRing, but since Ring is the only type exported by the package, and since the package is called ring, it's called just New, which clients of the package see as ring.New

    Original Answer

    The import path should reference a directory. In your code, you then reference that package by whatever name is used in the package [name] in the .go files in that directory.

    For example, if github.com/lrsmith/go-icinga2-api contains a file called api.go with the line package iapi, your importing package should look like this:

    package main
    import (
       "github.com/lrsmith/go-icinga2-api"
    )
    func main () {
       t := iapi.Config("zaphod","beeblebrox","http://localhost",true)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集