This repository has been archived and deprecated as the upstream IPFS switched over to Go modules, making it possible to finally use IPFS from Go in an officially supported way!
Head over to https://github.com/ipfs/go-ipfs for the new code (minimum version of v0.4.20-rc1
is needed for Go modules).
This repository is an unofficial fork of github.com/ipfs/go-ipfs, converted from a gx
based project to a plain Go project. The goal is to act as an IPFS library that can be imported and used from Go apps without the need of switching all dependency management over to gx
. As a bonus, this fork is compatible with GoDoc!
For a rundown of why gx
is not the best solution at the moment, please see the rationale section behind the ungx
project.
Upstream go-ipfs
is both a gx
based project, as well as depends on many third party gx
based packages. To use it in plain Go projects, all gx
packages need to be resolved into their original canonical versions, or need to be converted into non-gx ones.
This fork uses the following logic to ungx
go-ipfs:
- If a dependency has a plain Go canonical version (e.g.
golang.org/x/net
), the dependency is converted from an IPFS multihash into its canonical path and vendored into the standardvendor
folder. This ensures they play nice with the usual package managers. - If a dependency is only available as a
gx
project (e.g.github.com/libp2p/go-libp2p
), the dependency is converted from an IPFS multihash into its canonical path, but is moved into thegxlibs
folder within the main repository. This ensures external packages can import them.
Two caveats were also needed to enable this fork:
- If multiple versions of the same plain Go dependency is found, these cannot be vendored in. In such cases, all clashing dependencies are embedded into the
gxlibs/gx/ipfs
folder with their original IPFS multihashes. This retains the original behavior whilst still permitting imports. - If an embedded dependency contains canonical path constraints (e.g.
golang.org/x/sys/unix
), these constraints are blindly deleted from the dependency sources. Unfortunately this is the only way to allow external code to import them without Go failing the build.
The ungx-ing process is done automatically for the master
branch in a nightly Travis cron job from the ungx
branch in this repository. Upstream releases (i.e. tags) are not yet ungx-ed to prevent having to re-tag versions if a bug in ungx
is discovered. Those will be added and tagged when the process is deemed reliable enough.
The hello-world of IPFS is retrieving the official welcome page from the network. With the IPFS command line client this looks something like:
$ ipfs cat QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB
Hello and Welcome to IPFS!
██╗██████╗ ███████╗███████╗
██║██╔══██╗██╔════╝██╔════╝
██║██████╔╝█████╗ ███████╗
██║██╔═══╝ ██╔══╝ ╚════██║
██║██║ ██║ ███████║
╚═╝╚═╝ ╚═╝ ╚══════╝
[...]
Doing the same thing from Go is a bit more involved as it entails creating an ephemeral in-process IPFS node and using that as a gateway to retrieve the welcome page:
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"github.com/ipsn/go-ipfs/core"
"github.com/ipsn/go-ipfs/core/coreapi"
"github.com/ipsn/go-ipfs/core/coreapi/interface"
)
func main() {
// Create a new IPFS network node
node, err := core.NewNode(context.TODO(), &core.BuildCfg{Online: true})
if err != nil {
log.Fatalf("Failed to start IPFS node: %v", err)
}
path, _ := iface.ParsePath("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB")
// Resolve the IPFS welcome page
reader, err := coreapi.NewCoreAPI(node).Unixfs().Get(context.TODO(), path)
if err != nil {
log.Fatalf("Failed to look up IPFS welcome page: %v", err)
}
// Retrieve and print the welcome page
blob, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatalf("Failed to retrieve IPFS welcome page: %v", err)
}
fmt.Println(string(blob))
}
However, after the dependencies are met, our pure Go IPFS code works flawlessly:
$ go get -v github.com/ipsn/go-ipfs/core
$ go run ipfs.go
Hello and Welcome to IPFS!
██╗██████╗ ███████╗███████╗
██║██╔══██╗██╔════╝██╔════╝
██║██████╔╝█████╗ ███████╗
██║██╔═══╝ ██╔══╝ ╚════██║
██║██║ ██║ ███████║
╚═╝╚═╝ ╚═╝ ╚══════╝
[...]
Although the above demo works correctly, running go get -v github.com/ipsn/go-ipfs/core
is not for the faint of heart. It will place about 1193 packages into you GOPATH
😱. A much better solution is to use your favorite dependency manager!
Demo with govendor
:
$ go get -u github.com/kardianos/govendor
$ govendor init
$ govendor fetch -v +missing
$ go run ipfs.go
[...]
This repository is maintained by Péter Szilágyi (@karalabe), but authorship of all code contained inside belongs to the upstream go-ipfs project.
Same as upstream (MIT).