Skip to content
Snippets Groups Projects
Select Git revision
  • b370cb13bcd32d4f4f9a60b66259577e4005ad18
  • master default protected
  • fix/10837-provide-according-to-strategy
  • fix/add-api-v0-log--get-level
  • no-goprocess
  • feat-mainnet-autoconfig
  • telemetry-plugin2
  • spellcheck
  • docs-release-checklist-037
  • release
  • release-v0.36.0
  • telemetry-plugin
  • reprovide-sweep
  • fix-editor-env-handling
  • ux-acc-dht-note
  • fix-flush-files-rm
  • unixfs-percent-encoding-poc
  • fix-flaky-verify-test
  • fix/systemd-path
  • release-v0.35.0
  • staging
  • v0.36.0
  • v0.36.0-rc2
  • v0.36.0-rc1
  • v0.35.0
  • v0.35.0-rc2
  • v0.35.0-rc1
  • v0.34.1
  • v0.34.0
  • v0.34.0-rc2
  • v0.34.0-rc1
  • v0.33.2
  • v0.33.1
  • v0.33.0
  • v0.33.0-rc3
  • v0.33.0-rc2
  • v0.33.0-rc1
  • v0.32.1
  • v0.32.0
  • v0.32.0-rc2
  • v0.32.0-rc1
41 results

go-ipfs-as-a-library

  • Clone with SSH
  • Clone with HTTPS
  • user avatar
    Henrique Dias authored and GitHub committed
    b370cb13
    History
    Name Last commit Last update
    ..
    example-folder
    README.md
    main.go

    Use go-ipfs as a library to spawn a node and add a file

    This tutorial is the sibling of the js-ipfs IPFS 101 tutorial.

    By the end of this tutorial, you will learn how to:

    • Spawn an IPFS node that runs in process (no separate daemon process)
    • Create an IPFS repo
    • Add files and directories to IPFS
    • Retrieve those files and directories using cat and get
    • Connect to other nodes in the network
    • Retrieve a file that only exists on the network
    • The difference between a node in DHT client mode and full DHT mode

    All of this using only golang!

    In order to complete this tutorial, you will need:

    Disclaimer: The example code is quite large (more than 300 lines of code) and it has been a great way to understand the scope of the go-ipfs Core API, and how it can be improved to further the user experience. You can expect to be able to come back to this example in the future and see how the number of lines of code have decreased and how the example have become simpler, making other go-ipfs programs simpler as well.

    Getting started

    Note: Make sure you have installed.

    Download go-ipfs and jump into the example folder:

    > go get -u github.com/ipfs/go-ipfs
    cd $GOPATH/src/github.com/ipfs/go-ipfs/docs/examples/go-ipfs-as-a-library

    Running the example as-is

    To run the example, simply do:

    > go run main.go

    You should see the following as output:

    -- Getting an IPFS node running --
    Spawning node on a temporary repo
    IPFS node is running
    
    -- Adding and getting back files & directories --
    Added file to IPFS with CID /ipfs/QmV9tSDx9UiPeWExXEeH6aoDvmihvx6jD5eLb4jbTaKGps
    Added directory to IPFS with CID /ipfs/QmdQdu1fkaAUokmkfpWrmPHK78F9Eo9K2nnuWuizUjmhyn
    Got file back from IPFS (IPFS path: /ipfs/QmV9tSDx9UiPeWExXEeH6aoDvmihvx6jD5eLb4jbTaKGps) and wrote it to ./example-folder/QmV9tSDx9UiPeWExXEeH6aoDvmihvx6jD5eLb4jbTaKGps
    Got directory back from IPFS (IPFS path: /ipfs/QmdQdu1fkaAUokmkfpWrmPHK78F9Eo9K2nnuWuizUjmhyn) and wrote it to ./example-folder/QmdQdu1fkaAUokmkfpWrmPHK78F9Eo9K2nnuWuizUjmhyn
    
    -- Going to connect to a few nodes in the Network as bootstrappers --
    Fetching a file from the network with CID QmUaoioqU7bxezBQZkUcgcSyokatMY71sxsALxQmRRrHrj
    Wrote the file to ./example-folder/QmUaoioqU7bxezBQZkUcgcSyokatMY71sxsALxQmRRrHrj
    
    All done! You just finalized your first tutorial on how to use go-ipfs as a library

    Understanding the example

    In this example, we add a file and a directory with files; we get them back from IPFS; and then we use the IPFS network to fetch a file that we didn't have in our machines before.

    Each section below has links to lines of code in the file main.go. The code itself will have comments explaining what is happening for you.

    The func main() {}

    The main function is where the magic starts, and it is the best place to follow the path of what is happening in the tutorial.

    Part 1: Getting an IPFS node running

    To get get a node running as an ephemeral node (that will cease to exist when the run ends), you will need to:

    As soon as you construct the IPFS node instance, the node will be running.

    Part 2: Adding a file and a directory to IPFS

    Part 3: Getting the file and directory you added back

    Part 4: Getting a file from the IPFS network

    Bonus: Spawn a daemon on your existing IPFS repo (on the default path ~/.ipfs)

    As a bonus, you can also find lines that show you how to spawn a node over your default path (~/.ipfs) in case you had already started a node there before. To try it:

    Voilá! You are now a go-ipfs hacker

    You've learned how to spawn a go-ipfs node using the go-ipfs core API. There are many more methods to experiment next. Happy hacking!