Failed to build the echo example

Hi there, I’m very new to go-libp2p and I’m having a problem building the echo example code with the go version 1.17.6 linux/amd64 on a local machine. I was trying out the code with the following commands go mod init main, go mod tidy, go mod download and go build, but it returned the errors below. I don’t know what I have done wrong and I couldn’t find any solutions online, so hope someone could give me a hint of how to fix the errors. Cheers.

# github.com/libp2p/go-stream-muxer-multistream
/home/root/go/pkg/mod/github.com/libp2p/go-stream-muxer-multistream@v0.3.0/multistream.go:74:20: not enough arguments in call to tpt.NewConn
        have (net.Conn, bool)
        want (net.Conn, bool, network.PeerScope)
# github.com/libp2p/go-libp2p-yamux
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-yamux@v0.7.0/transport.go:49:5: cannot use &Transport{} (type *Transport) as type network.Multiplexer in assignment:
        *Transport does not implement network.Multiplexer (wrong type for NewConn method)
                have NewConn(net.Conn, bool) (network.MuxedConn, error)
                want NewConn(net.Conn, bool, network.PeerScope) (network.MuxedConn, error)
# github.com/libp2p/go-libp2p-mplex
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-mplex@v0.4.1/transport.go:22:5: cannot use &Transport{} (type *Transport) as type network.Multiplexer in assignment:
        *Transport does not implement network.Multiplexer (wrong type for NewConn method)
                have NewConn(net.Conn, bool) (network.MuxedConn, error)
                want NewConn(net.Conn, bool, network.PeerScope) (network.MuxedConn, error)
# github.com/libp2p/go-libp2p-transport-upgrader
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-transport-upgrader@v0.6.0/listener.go:108:46: undefined: transport.AcceptTimeout
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-transport-upgrader@v0.6.0/upgrader.go:118:2: cannot use tc (type *transportConn) as type transport.CapableConn in return argument:
        *transportConn does not implement transport.CapableConn (missing Scope method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-transport-upgrader@v0.6.0/upgrader.go:136:32: not enough arguments in call to u.Muxer.NewConn
        have (net.Conn, bool)
        want (net.Conn, bool, network.PeerScope)
# github.com/libp2p/go-libp2p-swarm
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/limiter.go:34:13: undefined: transport.DialTimeout
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm.go:233:43: cannot use c (type *Conn) as type network.Conn in argument to s.gater.InterceptUpgraded:
        *Conn does not implement network.Conn (missing Scope method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm.go:274:14: cannot use s (type *Swarm) as type network.Network in argument to f.Connected:
        *Swarm does not implement network.Network (missing ResourceManager method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm.go:274:14: cannot use c (type *Conn) as type network.Conn in argument to f.Connected:
        *Conn does not implement network.Conn (missing Scope method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm.go:356:13: cannot use c (type *Conn) as type network.Conn in assignment:
        *Conn does not implement network.Conn (missing Scope method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm.go:449:18: cannot use c (type *Conn) as type network.Conn in append:
        *Conn does not implement network.Conn (missing Scope method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm.go:567:5: cannot use (*Swarm)(nil) (type *Swarm) as type network.Network in assignment:
        *Swarm does not implement network.Network (missing ResourceManager method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm.go:568:5: cannot use (*Swarm)(nil) (type *Swarm) as type transport.TransportNetwork in assignment:
        *Swarm does not implement transport.TransportNetwork (missing ResourceManager method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm_conn.go:85:20: cannot use c.swarm (type *Swarm) as type network.Network in argument to f.Disconnected:
        *Swarm does not implement network.Network (missing ResourceManager method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm_stream.go:15:5: cannot use &Stream{} (type *Stream) as type network.Stream in assignment:
        *Stream does not implement network.Stream (missing Scope method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-swarm@v0.9.0/swarm_conn.go:85:20: too many errors
# github.com/libp2p/go-libp2p-quic-transport
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-quic-transport@v0.15.2/conn.go:28:5: cannot use &conn{} (type *conn) as type transport.CapableConn in assignment:
        *conn does not implement transport.CapableConn (missing Scope method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-quic-transport@v0.15.2/listener.go:84:21: cannot use conn (type *conn) as type transport.CapableConn in send:
        *conn does not implement transport.CapableConn (missing Scope method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-quic-transport@v0.15.2/listener.go:92:3: cannot use conn (type *conn) as type transport.CapableConn in return argument:
        *conn does not implement transport.CapableConn (missing Scope method)
/home/root/go/pkg/mod/github.com/libp2p/go-libp2p-quic-transport@v0.15.2/transport.go:234:2: cannot use conn (type *conn) as type transport.CapableConn in return argument:
        *conn does not implement transport.CapableConn (missing Scope method)

Okay. I’ve figured out the problem. It was the version issue of the go-libp2p-core in go.mod file.

Previously, the github.com/libp2p/go-libp2p-core v0.14.0 was downloaded in the go.mod, but the current example code is using v0.13.0, which causes compile errors.

Solutions: Just simply edit the go.mod file, changing from github.com/libp2p/go-libp2p-core v0.14.0 to github.com/libp2p/go-libp2p-core v0.13.0, before running go mode tidy and go build. Then, it runs smoothly.