Build a Blockchain in Golang from Scratch – Beginner Guide

In this article, we will be focusing on Go blockchain development from scratch. However, before we get started, you should be sure that you are familiar with the basic concepts in Golang. If not, then it is wise if you go through the preliminary concepts and then come back to blockchain. 

So, let us get straight to the topic. 

Starting with a New Directory 

We will build a new directory to begin with. Let us assume that this directory has the name “blockchain.” We will type the code in the Command Prompt (or if you are using macOS or Linux, you have to use the Terminal). Thus, we type:

cd go-workspace 

mkdir blockchain 

cd blockchain 

code .

As the VS Code opens, we will create a Go module in the Command Prompt. How do we do so? Well, we type:

go mod init github.com/golang-company/blockchain

Coding in main.go 

Next, we will create a Go source file named ‘main.go’ and we will type the code in it. But first let us understand what blockchain is. A blockchain may be defined as a public database that is decentralized and distributed among several peers. Blockchain allows the database to self-correct, even if a node is producing inaccurate data. 

Usually, a block on a blockchain consists of data we share in the database, a hash, and the previous block’s cryptographic hash. 

 So, are you ready for Go blockchain development? Great! Let’s get started. 

Programming Part 

In this section, we will look into the main.go file. 

package main

import (

“bytes”

“crypto/sha256” 

“fmt” 

)

type Cryptoblock struct {

Hash [] byte 

Data [] byte 

PrevHash [] byte 

}

  • As you can see, a struct has only been created. 

func (c *Cryptoblock) BuildHash() {

details := bytes.Join([][] byte{c.Data, c.PrevHash}, []byte{}) 

hash := sha256.Sum256(details)

c.Hash = hash[ : ]

}

  • We will now construct a method that will enable us to generate a hash depending on the data and the previous hash. We shall import the “bytes” library because we will be using it.
  • The next step is to create a variable called details and use the data type bytes. We will use Join() to connect the slices of bytes.

details := bytes.Join([][] byte{c.Data, c.PrevHash}, []byte{}) 

Here, we are taking a 2D slice of bytes, we transmit the c.Data and the previous hash. Then we will combine the empty slice of bytes. 

  • Following this, we are creating the actual hash taking help of the sum256 hashing function on the details. We can use this as we will import the sha256 library. 
  • Next, we push the created hash into the Hash field for the block. 

func BuildBlock (data string, prevHash [] byte) *Cryptoblock {

block := &Cryptoblock{[]byte{}, []byte(data), prevHash}

block.BuildHash()

return block

  • We will now build a function that enables the creation of Block. The function accepts a string of data as input, prevHash from the previous block as input, and then outputs a reference to Cryptoblock. We will build the block using the block constructor. 
  • The &Cryptoblock acts as the reference to the block. For Hash field, we incorporate an empty slice of bytes. For Data field, we take the data string and convert it into slice of bytes. And we incorporate prevHash into PrevHash field. 
  • Lastly, we call the BuildHash() on the block and we return the block. 

type BlockChain struct {

blocks []*Cryptoblock

}

  • A type that will help in expressing the blockchain is required. And we implemented a struct to accomplish this. The type BlockChain struct is made up of an array of pointers to Cryptoblock.

func (chain *BlockChain) AddBlock(data string) {

prevBlock := chain.blocks[len(chain.blocks)-1]

new := BuildBlock(data, prevBlock.Hash)

chain.blocks = append(chain.blocks, new)

}

  • Here, we are creating a method enabling us to join a block to the chain. The method retrieves the blockchain pointer. Following this, it accepts a data string. 
  • Calling chain.blocks, we get to the previous block in the blockchain. Next, we passed the length of the blockchain [len(chain.blocks)-1].
  • In the new variable, we are calling the BuildBlock function and we are passing data string and prevBlock.Hash.
  • By utilizing the append function, adding this to the chain.blocks, we then attach the new block to the blockchain.

func Inception() *Cryptoblock {

return BuildBlock(“Inception”, []byte{})

}

  • The next step is to create a function called Inception that will describe the blockchain’s first block. And we will return a new BuildBlock in the function, along with the data in the first block. Here. I’ve incorporated “Inception” and a slice of bytes which represents an empty previous hash. 

func InitBlockChain() *BlockChain {

return &BlockChain{[]*Cryptoblock{Inception()}}

}

  • In order to create the first blockchain, I have introduced the InitBlockChain function. Here, I’m just returning the particular reference to the BlockChain. Next, we build an array of Cryptoblock, where we make a call to the Inception function. 

func main() {

chain := InitBlockChain()

chain.AddBlock(“First Block after Inception”)

chain.AddBlock(“Second Block after Inception”)

chain.AddBlock(“Third Block after Inception”)

for _, block := range chain.blocks {

fmt.Printf(“Previous Hash: %x\n”, block.PrevHash)

fmt.Printf(“Data in Block: %s\n”, block.Data)

fmt.Printf(“Hash: %x\n”, block.Hash)

}

}

  • Finally, we have arrived at the main function. As you can see we have called the InitBlockChain() and we have assigned it to the chain variable. 
  • Next, we are adding blocks to the chain via chain.AddBlock, and we pass the necessary data. 
  • Following this, we run a for loop to check for the blockchain. Then we single out each block and print the fields inside each block. We just type:

fmt.Printf(“Previous Hash: %x\n”, block.PrevHash)

fmt.Printf(“Data in Block: %s\n”, block.Data)

fmt.Printf(“Hash: %x\n”, block.Hash)

Output: 

So, we can say that the program is successful. I hope you were able to understand the implementation of the concept of blockchain in Golang. Just keep on practicing and you will be able to handle intricate projects. 

Your crypto deserves the best security. Get a Ledger hardware wallet for just $79!

  • bitcoinBitcoin (BTC) $ 16,996.20 0.17%
  • ethereumEthereum (ETH) $ 1,283.27 0.46%
  • bnbBNB (BNB) $ 290.46 0.71%
  • xrpXRP (XRP) $ 0.391729 1.84%
  • solanaSolana (SOL) $ 13.62 0.17%
  • terra-luna-2Terra (LUNA) $ 1.66 3.38%