不能运行aerospike去的例子

时间:2017-07-16 07:10:03

标签: go aerospike

我正在尝试运行一个空中飞行的例子:

package main

    import (
    "github.com/aerospike/aerospike-client-go"
    "fmt"
    )

    func panicOnError(err error) {
    if err != nil {
        panic(err)
    }
    }

     func main() {
    // define a client to connect to
    client, err := NewClient("127.0.0.1", 3000)
    panicOnError(err)

    key, err := NewKey("test", "aerospike", "key")
    panicOnError(err)

    // define some bins with data
    bins := BinMap{
        "bin1": 42,
        "bin2": "An elephant is a mouse with an operating system",
        "bin3": []interface{}{"Go", 2009},
    }

    // write the bins
    err = client.Put(nil, key, bins)
    panicOnError(err)

    // read it back!
    rec, err := client.Get(nil, key)
    panicOnError(err)

    fmt.Printf("%#v\n", *rec)

    // delete the key, and check if key exists
    existed, err := client.Delete(nil, key)
    panicOnError(err)
    fmt.Printf("Record existed before delete? %v\n", existed)
}

但是我收到了一个错误:

Unresolved reference NewClient... 
and many more...

我运行命令:

go get github.com/aerospike/aerospike-client-go

它已将软件包下载到磁盘上。

你能帮忙吗?

1 个答案:

答案 0 :(得分:7)

您可以在项目aerospike/aerospike-client-go中看到example_listiter_int_test.go等测试:

  • 导入项目:

    as "github.com/aerospike/aerospike-client-go"
    
  • 使用具有正确前缀的NewClient:

    var v as.Value = as.NewValue(myListInt([]int{1, 2, 3}))
    

所以不要忘记加上NewClient前缀。

在你的情况下:

import (
as "github.com/aerospike/aerospike-client-go"
"fmt"
)

client, err := as.NewClient("127.0.0.1", 3000)

as是包名称的别名,因为,如“Call a function from another package in Go”中所述:

  

您可以通过导入路径导入包,并通过包名称引用所有导出的符号(以大写字母开头的符号),

package aerospike的{​​{3}}起,另一种选择是:

client, err := aerospike.NewClient("127.0.0.1", 3000)