golang中container/list包用法

本文介绍了Golang中的container/list包,它提供了一个双向链表的数据结构。内容包括List的各种操作方法,如初始化、添加元素、删除元素、移动元素等,并通过实例演示了如何使用这些方法。

list是一个双向链表。该结构具有链表的所有功能。

type Element

type Element struct {

        Value interface{}   //在元素中存储的值
}

func (e *Element) Next() *Element  //返回该元素的下一个元素,如果没有下一个元素则返回nil
func (e *Element) Prev() *Element//返回该元素的前一个元素,如果没有前一个元素则返回nil。

type List
func New() *List //返回一个初始化的list
func (l *List) Back() *Element //获取list l的最后一个元素
func (l *List) Front() *Element //获取list l的第一个元素
func (l *List) Init() *List  //list l初始化或者清除list l
func (l *List) InsertAfter(v interface{}, mark *Element) *Element  //在list l中元素mark之后插入一个值为v的元素,并返回该元素,如果mark不是list中元素,则list不改变。
func (l *List) InsertBefore(v interface{}, mark *Element) *Element//在list l中元素mark之前插入一个值为v的元素,并返回该元素,如果mark不是list中元素,则list不改变。
func (l *List) Len() int //获取list l的长度
func (l *List) MoveAfter(e, mark *Element)  //将元素e移动到元素mark之后,如果元素e或者mark不属于list l,或者e==mark,则list l不改变。
func (l *List) MoveBefore(e, mark *Element)//将元素e移动到元素mark之前,如果元素e或者mark不属于list l,或者e==mark,则list l不改变。
func (l *List) MoveToBack(e *Element)//将元素e移动到list l的末尾,如果e不属于list l,则list不改变。
func (l *List) MoveToFront(e *Element)//将元素e移动到list l的首部,如果e不属于list l,则list不改变。
func (l *List) PushBack(v interface{}) *Element//在list l的末尾插入值为v的元素,并返回该元素。
func (l *List) PushBackList(other *List)//在list l的尾部插入另外一个list,其中l和other可以相等。
func (l *List) PushFront(v interface{}) *Element//在list l的首部插入值为v的元素,并返回该元素。
func (l *List) PushFrontList(other *List)//在list l的首部插入另外一个list,其中l和other可以相等。
func (l *List) Remove(e *Element) interface{}//如果元素e属于list l,将其从list中删除,并返回元素e的值。

举例说明其用法。

package main

import (
	"container/list"
	"fmt"
)

func main() {
	l := list.New() //创建一个新的list
	for i := 0; i < 5; i++ {
		l.PushBack(i)
	}
	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Print(e.Value) //输出list的值,01234
	}
	fmt.Println("")
	fmt.Println(l.Front().Value) //输出首部元素的值,0
	fmt.Println(l.Back().Value)  //输出尾部元素的值,4
	l.InsertAfter(6, l.Front())  //首部元素之后插入一个值为10的元素
	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Print(e.Value) //输出list的值,061234
	}
	fmt.Println("")
	l.MoveBefore(l.Front().Next(), l.Front()) //首部两个元素位置互换
	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Print(e.Value) //输出list的值,601234
	}
	fmt.Println("")
	l.MoveToFront(l.Back()) //将尾部元素移动到首部
	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Print(e.Value) //输出list的值,460123
	}
	fmt.Println("")
	l2 := list.New()
	l2.PushBackList(l) //将l中元素放在l2的末尾
	for e := l2.Front(); e != nil; e = e.Next() {
		fmt.Print(e.Value) //输出l2的值,460123
	}
	fmt.Println("")
	<span style="color:#FF0000;">l.Init()           //清空l</span>
	fmt.Print(l.Len()) //0
	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Print(e.Value) //输出list的值,无内容
	}

}



root@ubuntu:/home/uu/go/src/github.com/hyperledger/fabric-samples/test-network# sudo ./network.sh deployCC Using docker and docker-compose deploying chaincode on channel 'mychannel' executing with the following - CHANNEL_NAME: mychannel - CC_NAME: basic - CC_SRC_PATH: ../asset-transfer-basic/chaincode-go - CC_SRC_LANGUAGE: go - CC_VERSION: 1.0 - CC_SEQUENCE: auto - CC_END_POLICY: NA - CC_COLL_CONFIG: NA - CC_INIT_FCN: NA - DELAY: 3 - MAX_RETRY: 5 - VERBOSE: false executing with the following - CC_NAME: basic - CC_SRC_PATH: ../asset-transfer-basic/chaincode-go - CC_SRC_LANGUAGE: go - CC_VERSION: 1.0 Vendoring Go dependencies at ../asset-transfer-basic/chaincode-go /home/uu/go/src/github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go /home/uu/go/src/github.com/hyperledger/fabric-samples/test-network /home/uu/go/src/github.com/hyperledger/fabric-samples/test-network Finished vendoring Go dependencies + '[' false = true ']' + peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go --lang golang --label basic_1.0 + res=0 Chaincode is packaged Installing chaincode on peer0.org1... Using organization 1 + jq -r 'try (.installed_chaincodes[].package_id)' + peer lifecycle chaincode queryinstalled --output json + grep '^Perform chaincode operations: package|install|queryinstalled|getinstalledpackage|approveformyorg|queryapproved|checkcommitreadiness|commit|querycommitted Usage: peer lifecycle chaincode [command] Available Commands: approveformyorg Approve the chaincode definition for my org. checkcommitreadiness Check whether a chaincode definition is ready to be committed on a channel. commit Commit the chaincode definition on the channel. getinstalledpackage Get an installed chaincode package from a peer. install Install a chaincode. package Package a chaincode queryapproved Query an org'\''s approved chaincode definition from its peer. querycommitted Query the committed chaincode definitions by channel on a peer. queryinstalled Query the installed chaincodes on a peer. Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) -h, --help help for chaincode --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer --tls Use TLS when communicating with the orderer endpoint --tlsHandshakeTimeShift duration The amount of time to shift backwards for certificate expiration checks during TLS handshakes with the orderer endpoint Use "peer lifecycle chaincode [command] --help" for more information about a command.$' + test 1 -ne 0 + peer lifecycle chaincode install basic.tar.gz + res=1 Error: chaincode install failed with status: 500 - failed to invoke backing implementation of 'InstallChaincode': could not build chaincode: docker build failed: docker image build failed: docker build failed: Error returned from build: 1 "vendor/google.golang.org/protobuf/internal/editiondefaults/defaults.go:9:8: cannot find package "." in: /chaincode/input/src/vendor/embed vendor/golang.org/x/net/http2/transport.go:19:2: cannot find package "." in: /chaincode/input/src/vendor/io/fs vendor/google.golang.org/grpc/stats/metrics.go:19:8: cannot find package "." in: /chaincode/input/src/vendor/maps vendor/google.golang.org/grpc/balancer/pickfirst/internal/internal.go:22:2: cannot find package "." in: /chaincode/input/src/vendor/math/rand/v2 vendor/google.golang.org/grpc/balancer/pickfirst/pickfirstleaf/pickfirstleaf.go:33:2: cannot find package "." in: /chaincode/input/src/vendor/net/netip vendor/google.golang.org/grpc/clientconn.go:27:2: cannot find package "." in: /chaincode/input/src/vendor/slices " Chaincode installation on peer0.org1 has failed Deploying chaincode failed 发生了什么错误
06-11
### Hyperledger Fabric 链码部署失败的常见原因及解决方法 Hyperledger Fabric 中链码部署失败可能由多种原因引起,括但不限于构建环境配置问题、依赖项缺失或版本不兼容等。以下是对该问题的具体分析和解决方案: #### 1. **Docker 构建失败** 当链码构建过程中出现 `docker build failed` 错误时,通常是因为 Docker 环境未正确配置或链码代码中存在编译错误。根据引用内容[^1],如果链码依赖了外部库(如 `google.golang.org/protobuf`),但这些库未被正确打到项目中,则会导致构建失败。 **解决方法:** - 确保链码项目的根目录下含完整的 `vendor` 文件夹,其中含所有必要的依赖项。 - 使用 `go mod vendor` 命令生成 `vendor` 文件夹验证其完整性[^3]。 ```bash go mod tidy go mod vendor ``` #### 2. **Docker Socket 连接问题** 在 macOS 上运行 Docker Desktop 时,可能会遇到类似 `dial unix /host/var/run/docker.sock: connect: no such file or directory` 的错误[^2]。这是由于 Docker 容器无法访问主机的 Docker Socket。 **解决方法:** - 修改 Docker Compose 文件或启动脚本,确保容器能够访问 Docker Socket。 - 在 Docker Compose 文件中添加以下配置: ```yaml volumes: - /var/run/docker.sock:/host/var/run/docker.sock ``` - 或者,在启动 Peer 节点时显式挂载 Docker Socket: ```bash docker run -v /var/run/docker.sock:/host/var/run/docker.sock ... ``` #### 3. **链码安装命令执行失败** 如果链码安装命令 `peer lifecycle chaincode install` 执行失败,可能是由于链码路径错误或 Peer 节点状态异常[^3]。 **解决方法:** - 验证链码路径是否正确,确保文件存在。 - 检查 Peer 节点的日志以获取更多详细信息: ```bash docker logs <peer-container-name> ``` #### 4. **链码构建依赖问题** 如果链码使用了 Go 语言编写,而构建过程中因依赖项缺失导致失败,则需要确保所有依赖项都被正确解析和打。 **解决方法:** - 在链码项目根目录下运行以下命令以验证依赖项: ```bash go list -m all ``` - 如果发现某些依赖项未被正确解析,可以尝试手动更新 `go.mod` 文件或重新生成 `vendor` 文件夹。 --- ### 示例代码:打和安装链码 以下是打和安装链码的标准流程[^3]: ```bash # 打链码 peer lifecycle chaincode package sacc.tar.gz --path /opt/gopath/src/github.com/hyperledger/multiple-deployment/chaincode/go --label sacc_1 # 安装链码 peer lifecycle chaincode install sacc.tar.gz ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值