无法运行 Aerospike Go 示例。

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

cannot run aerospike go example

问题

我正在尝试运行一个 Aerospike Go 示例:

package main

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

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

func main() {
	// 定义一个连接客户端
	client, err := aerospike.NewClient("127.0.0.1", 3000)
	panicOnError(err)

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

	// 定义一些带有数据的 bins
	bins := aerospike.BinMap{
		"bin1": 42,
		"bin2": "An elephant is a mouse with an operating system",
		"bin3": []interface{}{"Go", 2009},
	}

	// 写入 bins
	err = client.Put(nil, key, bins)
	panicOnError(err)

	// 读取数据!
	rec, err := client.Get(nil, key)
	panicOnError(err)

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

	// 删除 key,并检查 key 是否存在
	existed, err := client.Delete(nil, key)
	panicOnError(err)
	fmt.Printf("Record existed before delete? %v\n", existed)
}

但是我遇到了一个错误:

Unresolved reference NewClient...
还有很多其他错误...

我已经运行了以下命令:

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

它已经在磁盘上下载了该包。

你需要帮忙吗?

英文:

I'm trying to run an aerospike go example:

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

But I get an error:

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

I've run the command:

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

and it has downloaded the package on the disk.

Can you help?

答案1

得分: 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是包名的别名,正如在“在Go中从另一个包中调用函数”中提到的:

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

由于NewClientclient.go属于aerospike包,另一种选择是:

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

You can see in the project aerospike/aerospike-client-go tests like example_listiter_int_test.go which:

  • import the project with:

      as "github.com/aerospike/aerospike-client-go"
    
  • use NewClient with the right prefix:

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

So don't forget to prefix NewClient.

In your case:

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

And:

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

as is an alias for the package name, since, as mentioned in "Call a function from another package in Go":

> You import the package by its import path, and reference all its exported symbols (those starting with a capital letter) through the package name,

Since NewClient is in client.go of the package aerospike, an alternative would be:

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

huangapple
  • 本文由 发表于 2017年7月16日 15:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/45126014.html
匿名

发表评论

匿名网友

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

确定