golang:自定义包和’undefined’

huangapple go评论87阅读模式
英文:

golang : Custom package and 'undefined'

问题

我已经阅读了关于创建自定义包的文档,但似乎无法确定问题出在哪里。

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

我已经执行了 'go get github.com/lrsmith/go-icinga2-api/iapi',它将其放入与 'zaphod' 相同的目录中,并在 pkg 下创建了一个 .a 文件。

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

zaphod.go 目前非常简单

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

当我在 zaphod 目录中执行 go build 时,我得到以下错误
./zaphod.go:11: undefined: iapi.Config

我已经阅读了文档,检查了大小写,并尝试了不同的结构,但似乎无法加载该包并让我调用 iapi.Config。iapi 代码是有效的,如果我在 go-icinga2-api 目录中构建某些东西,它可以正常工作并通过所有测试。

我想创建一个独立的项目/代码库,导入 go-icinga2-api 并使用它,但似乎无法使其工作。

谢谢
Len

#添加的信息。go-icinga2-api 的结构如下

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

client.go 是

// 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

}

我尝试过将 .go 文件放在上一级,即不嵌套在 iapi/ 下,但结果相同。

英文:

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.

答案1

得分: 2

更新的答案

client.go中,看起来你试图将Config作为Server结构体的构造函数使用。由于Config是使用接收器(func(server *Server))定义的,它是Server的方法,不能直接调用。如果你删除(server *Server),你的代码应该可以工作。

按照惯例,构造函数的命名应为New[返回的类型],或者如果类型与包的名称相同,则为New

来自《Go 语言的惯用法》中的《包名称》一节的第三段:
> 用于创建 ring.Ring 的新实例的函数——这是 Go 中构造函数的定义——通常被称为 NewRing,但由于 Ring 是包导出的唯一类型,并且由于该包的名称为 ring,所以它被称为 ring.New,客户端看到的是 ring.New

原始答案

导入路径应该引用一个目录。然后,在你的代码中,你可以使用在该目录中的.go文件中的package [name]中使用的任何名称来引用该包。

例如,如果github.com/lrsmith/go-icinga2-api包含一个名为api.go的文件,其中包含一行package iapi,你的导入包应该像这样:

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

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)
}

答案2

得分: 1

请注意Config()函数的声明:

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

这是一个应该应用于Server对象的"方法":

server.Config(...)

因此,你需要首先创建一个Server对象(或者你可以尝试使用nil):

var server iapi.Server
server, err := server.Config(...)

你试图将其运行,就好像它有以下声明:

func Config(username, password, url string, allowUnverifiedSSL bool) (*Server, error)
英文:

Note the declaration of the Config() function:

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

It's a "method" that should be applied to a Server object:

server.Config(...)

Thus you need to first create a Server object (or you could try with nil):

var server iapi.Server
server, err := server.Config(...)

You're trying to run it as if it had the following declaration:

func Config(username, password, url string, allowUnverifiedSSL bool) (*Server, error)

huangapple
  • 本文由 发表于 2016年11月2日 11:44:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/40372146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定