# Using Go-IPFS Programmatically

**URL:** https://discuss.ipfs.tech/t/using-go-ipfs-programmatically/3784
**Category:** Kubo
**Tags:** go-ipfs
**Created:** [September 5, 2018, 4:24pm UTC](https://discuss.ipfs.tech/t/using-go-ipfs-programmatically/3784 "2018-09-05T16:24:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![cacciaguida](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@cacciaguida](https://discuss.ipfs.tech/u/cacciaguida)
#### Post date: [September 5, 2018, 4:24pm UTC](https://discuss.ipfs.tech/t/using-go-ipfs-programmatically/3784/1 "2018-09-05T16:24:16Z")

</div>

Can you guys answer this question?

> <https://stackoverflow.com/questions/52179737/using-go-ipfs-programmatically>

---

<div class="post-metadata">

### Author: ![postables](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/postables/32/2729_2.png) [@postables](https://discuss.ipfs.tech/u/postables)
#### Post date: [September 5, 2018, 11:04pm UTC](https://discuss.ipfs.tech/t/using-go-ipfs-programmatically/3784/2 "2018-09-05T23:04:52Z")

</div>

You’re probably better off using the `go-ipfs-api` for what you want:

> **[ipfs/go-ipfs-api](https://github.com/ipfs/go-ipfs-api)**
>
> The go interface to ipfs's HTTP API. Contribute to ipfs/go-ipfs-api development by creating an account on GitHub.

If you want some examples on working with IPFS using Golang, feel free to check out these folders of my project:  
[https://github.com/RTradeLtd/Temporal/tree/V2/rtfs](https://github.com/RTradeLtd/Temporal/tree/V2/rtfs)  
[https://github.com/RTradeLtd/Temporal/tree/V2/rtfs\_cluster](https://github.com/RTradeLtd/Temporal/tree/V2/rtfs_cluster)  
[https://github.com/RTradeLtd/Temporal/tree/V2/rtfsp](https://github.com/RTradeLtd/Temporal/tree/V2/rtfsp)  
[https://github.com/RTradeLtd/Temporal/tree/V2/rtns](https://github.com/RTradeLtd/Temporal/tree/V2/rtns)

---

<div class="post-metadata">

### Author: ![cacciaguida](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@cacciaguida](https://discuss.ipfs.tech/u/cacciaguida)
#### Post date: [September 6, 2018, 11:55am UTC](https://discuss.ipfs.tech/t/using-go-ipfs-programmatically/3784/3 "2018-09-06T11:55:07Z")

</div>

@postables Thank you very much for your help, however I really don’t like the API that’s the reason I am asking the question in the first place. I don’t want to be forced in this client-server architecture and I don’t understand why the want to do this to us. I want to be able to decide myself how to design my systems.

---

<div class="post-metadata">

### Author: ![postables](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/postables/32/2729_2.png) [@postables](https://discuss.ipfs.tech/u/postables)
#### Post date: [September 6, 2018, 5:26pm UTC](https://discuss.ipfs.tech/t/using-go-ipfs-programmatically/3784/4 "2018-09-06T17:26:04Z")

</div>

You’re doing the same thing right now that you would be doing with `go-ipfs-api` and running `ipfs-daemon`. I’m not sure how they are forcing us into a client server architecture? IPFS is a fairly intricate system, and IMO at least you’re better off using `go-ipfs-api` and play around with the IPFS daemon, then moving onto stuff like this.

Here you are starting a node

```auto
   node, err := core.NewNode(ctx, &core.BuildCfg{})
    if err != nil {
        log.Fatalf("Failed to start IPFS node: %v", err)
    }

```

And here you are running the cat command

```auto
    reader, err := coreunix.Cat(ctx, node, "QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB")
    if err != nil {
        log.Fatalf("Failed to look up IPFS welcome page: %v", err)
    }

```

Even in your own stack overflow post you mention wanting to basically re-do the same thing the `ipfs-daemon` does. I’m not sure why you would want to do this, as `go-ipfs` and the `ipfs-daemon` already handle all of this heavy lifting for you. You can simply run your own `ipfs-daemon` on your node, and have your golang program use `go-ipfs-api` to talk to it, rather than build your node dynamically the way you are now, and then talking to it.

EDIT: To answer some of your questions

Whenever you add a file to IPFS, the bitswap agent will search through a “want list” which is a list of blocks that your node wants. I believe it initially adds the file into memory, however you may want someone else to clear that part up.

As for your question on the differences of `context` that is more focused on Golang as opposed to IPFS [https://siadat.github.io/post/context](https://siadat.github.io/post/context)

I lightly annotated some of the function calls here

```auto
func addFile(ctx *context.Context, node *core.IpfsNode, path *string) error {
    file, err := os.Open(*path) <-- here we are opening up a file from disk
    if err != nil {
        return err
    }
    adder, err := coreunix.NewAdder(*ctx, node.Pinning, node.Blockstore, node.DAG) <- here we creating a method utilizing a blockstore which will allow us to store, and pin blocks
    if err != nil {
        return err
    }
    filename := filepath.Base(*path)
    fileReader := files.NewReaderFile(filename, filename, file, nil)
    adder.AddFile(fileReader) <-- here we are adding the file to our repo
    adder.PinRoot() <-- here we are pinning the root node
    return nil
}

```

I took a look at the function `AddFile` which in turn calls `addFile` and IIRC it streams the file into memory where it gets chunked and hashed before being flushed to disk as it chunks and hashes although someone else might want to clarify this:

> <https://github.com/ipfs/go-ipfs/blob/2dca8c2f965a4e4feb9df98f059f954de16846b6/core/coreunix/add.go#L414>

IPFS is pretty undocumented right now (but you can help with that! :D) I gathered some quick godoc links that will help to look at code to get a better understanding of the system to do what you’re doing:

> **[Package blockstoreutil](https://godoc.org/github.com/ipfs/go-ipfs/blocks/blockstoreutil)**
>
> Package blockstoreutil provides utility functions for Blockstores.

  
[https://godoc.org/github.com/ipfs/go-ipfs/core/coredag](https://godoc.org/github.com/ipfs/go-ipfs/core/coredag)  
[https://godoc.org/github.com/ipfs/go-ipfs/core/corerepo](https://godoc.org/github.com/ipfs/go-ipfs/core/corerepo)  

> **[Package blockstoreutil](https://godoc.org/github.com/ipfs/go-ipfs/blocks/blockstoreutil)**
>
> Package blockstoreutil provides utility functions for Blockstores.
