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.

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

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)