package main import ( "context" "fmt" dag "github.com/ipfs/go-ipfs/merkledag" "github.com/ipfs/go-ipfs/node" ) func main() { // Create the IPFS node ctx := context.Background() cfg := &node.BuildCfg{} nd, err := node.New(ctx, cfg) if err != nil { panic(err) } // Create a new DAG service dagService := dag.NewDAGService(nd.DAG) // Create a new node with some data data := []byte("Hello, world!") node := dag.NodeWithData(data) // Add the node to the DAG err = dagService.Add(ctx, node) if err != nil { panic(err) } fmt.Printf("Added node to DAG: %s\n", node.Cid()) }
package main import ( "context" "fmt" dag "github.com/ipfs/go-ipfs/merkledag" "github.com/ipfs/go-ipfs/node" ) func main() { // Create the IPFS node ctx := context.Background() cfg := &node.BuildCfg{} nd, err := node.New(ctx, cfg) if err != nil { panic(err) } // Create a new DAG service dagService := dag.NewDAGService(nd.DAG) // Retrieve a node by its CID cidStr := "QmSAYVvGtP6WhdwjoWbUGi84D5cLkBYMbijWMeK1hpA9hB" cid, err := cid.Decode(cidStr) if err != nil { panic(err) } node, err := dagService.Get(ctx, cid) if err != nil { panic(err) } // Print out the data in the retrieved node fmt.Printf("Node data: %s\n", string(node.Data())) }This code also creates an IPFS node and a DAG service, but it fetches a pre-existing node from the DAG using its CID. It prints out the data stored in the retrieved node. Overall, the go-ipfs.merkledag package provides a convenient layer on top of the lower-level IPLD DAG packages, allowing users to easily create and work with DAGs in their applications.