Go-ipfs: Adding some go.mod information along the example

Created on 2 Jan 2020  路  13Comments  路  Source: ipfs/go-ipfs

Location

docs/examples/go-ipfs-as-a-library

Description

I found the example clear and helpful but when it comes to applying it to a real project, it can be hard without information about go.mod , just adding some information about how to start a real project could really help the begginer.

dieasy help wanted topidocs-ipfs

Most helpful comment

We can actually just add a go.mod file to that subdirectory.

All 13 comments

We can actually just add a go.mod file to that subdirectory.

Can you provide some information on how to start a real project? I am struggling to get the example running outside of the project repository. I tried to copy the go.mod and go.sum files but still it's not working.

Can you provide some information on how to start a real project? I am struggling to get the example running outside of the project repository. I tried to copy the go.mod and go.sum files but still it's not working.

Can't help without details of what exactly is not working...

Thanks for reply @hsanjuan ,

I am a beginner with package management and I am doing something wrong here.

I want to run the example of using go-ipfs as a library outside of ipfs's folder from repository.

I have a separate folder %GOPATH%\src\github.com\example with

  • main.go
  • go.sum
  • go.mod

Main.go contains the example from using ipfs as a library.
Go.mod, I copied the contentsfrom the repository while changing the go version at the bottom to 1.14, changed the module name at the top and added an extra row for requirements:
github.com/ipfs/go-ipfs v0.5.0-dev

Go.sum is identical to the one on the ipfs repository.

I run go get inside the folder to retrieve all the dependencies and when I run the file go run main.goI get the following errors :

Complete error and also contents of go.mod and go.sum here

github.com/libp2p/go-libp2p-loggables
..libp2pgo-libp2p-loggablesloggables.go:19:26: undefined: log.Loggable
..libp2pgo-libp2p-loggablesloggables.go:20:9: undefined: log.Metadata
..libp2pgo-libp2p-loggablesloggables.go:27:21: undefined: log.Loggable
..libp2pgo-libp2p-loggablesloggables.go:28:9: undefined: log.Metadata
..libp2pgo-libp2p-loggablesloggables.go:33:23: undefined: log.Metadata
..libp2pgo-libp2p-loggablesloggables.go:38:9: undefined: log.Metadata
github.com/ipfs/go-ipfs-blockstore
..ipfsgo-ipfs-blockstoreblockstore.go:224:8: log.Warningf undefined (type *log.ZapEventLogger has no field or method Warningf)
..ipfsgo-ipfs-blockstorebloom_cache.go:37:8: log.Warning undefined (type *log.ZapEventLogger has no field or method Warning)
..ipfsgo-ipfs-blockstorebloom_cache.go:90:12: log.EventBegin undefined (type *log.ZapEventLogger has no field or method EventBegin)
github.com/ipfs/go-ipfs-provider/queue
..ipfsgo-ipfs-providerqueuequeue.go:99:10: log.Warningf undefined (type *log.ZapEventLogger has no field or method Warningf)

github.com/ipfs/go-ipfs v0.5.0-dev... Does dev have a special meaning in go mod? Maybe go get github.com/ipfs/go-ipfs@master.You are somehow requiring wrong versions.

I tried with both @master and @latest. I have updated the gist with the errors from the @latest tag. They are mostly the same issues.

I also tried to ditch the go.mod and go.sum files. I emptied the %GOPATH%\src\github.com folder. Created a new folder with just the main.go example file and ran go get and got same erros

I am using Windows 10 Version 10.0.18362 Build 18362 with Go 1.14. I see that in go.mod from ipfs-go repository go 1.12 is used. Should I try with that version instead ?

ok, this is what I do and I have no problems at all:

~ $ mkdir ipfs-example
~ $ cd ipfs-example/
~/ipfs-example $ cp -r ~/go/src/github.com/ipfs/go-ipfs/docs/examples/go-ipfs-as-a-library/* .
~/ipfs-example $ go mod init github.com/hsanjuan/ipfs-example
go: creating new go.mod: module github.com/hsanjuan/ipfs-example
~/ipfs-example $ go get github.com/ipfs/go-ipfs@master
go: github.com/ipfs/go-ipfs master => v0.4.22-0.20200318163333-6825097aeacc
~/ipfs-example $ go build
~/ipfs-example $ cat go.mod 
module github.com/hsanjuan/ipfs-example

go 1.14

require (
        github.com/ipfs/go-ipfs v0.4.22-0.20200318163333-6825097aeacc
        github.com/ipfs/go-ipfs-config v0.3.0
        github.com/ipfs/go-ipfs-files v0.0.6
        github.com/ipfs/interface-go-ipfs-core v0.2.6
        github.com/libp2p/go-libp2p-core v0.5.0
        github.com/libp2p/go-libp2p-peerstore v0.2.0
        github.com/multiformats/go-multiaddr v0.2.1
)

Thanks !

This also works for me.

I guess letting go searching all needed dependencies with just go get retrieves wrong versions.

Maybe I was just doing the wrongs steps.

@chelneru I am not a go expert, but my understanding is that go.mod is never to be edited by hand. You add/update things to it with go get <thing> and you remove stale entries from it with go mod tidy.

@ribasushi I didn't know that. It's a good thing to know. Thanks.
I tried a lot of ways to get this working and only with @hsanjuan steps it worked.

This should be mentioned in way in that wiki of ipfs-as-library file. Might help future beginners that want to try.

Hello, I can confim that @hsanjuan's fix worked for me as well.

I realize this must be quite low on the list of priorities, but FWIW this fix was somewhat hard to find. I'm happy to submit a PR if there's any interest. If so, are there any particular caveats or requirements for the PR to be aware of?

A PR would be very welcome! I'm not aware of any caveats at the moment.

@Stebalien Cheers! Please see https://github.com/ipfs/go-ipfs/pull/7146.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

magik6k picture magik6k  路  3Comments

Mikaela picture Mikaela  路  3Comments

0x6431346e picture 0x6431346e  路  3Comments

emelleme picture emelleme  路  3Comments

zignig picture zignig  路  3Comments