Editor’s note: This guide on how to build a blockchain with Go was last updated on 30 March 2023 to reflect changes to Go and include more information about using blockchains with Go. To learn more about Go, check out our guide to getting started with Go for frontend developers.
Blockchains are the underlying technology for many decentralized applications and cryptocurrencies. They are also applicable in many industries and are creating new job roles and opportunities for developers, artists, gamers, content writers, and many more.
This tutorial aims to teach you how blockchains work by guiding you through building one from scratch with Go. If you have heard of blockchains but are still confused about how they work, this article is for you. To follow and understand this tutorial, you will need working knowledge of Go, Go v1.x installed on your machine, and a Go development environment (e.g., text editor, IDE).
Jump ahead:
What is a blockchain?
A blockchain is a digital record of transactions distributed and shared among the nodes of a computer network. Each transaction in the blockchain is called a block and links to another with cryptography techniques.
You can also think of a blockchain as a special kind of distributed database. In this database, each record is unique and immutable, and every record (except the first) contains a reference to the record that came before it. Every record has a hash that identifies the record and ensures that any tampering done to it can be detected. Every device that’s a part of this network contains a copy of the database, and multiple devices have to agree that a record is valid using a consensus mechanism before it can be added to the database.
Here’s a visual example to illustrate the concept:
What is a block?
We mentioned blocks earlier, and you might be wondering what they are. Put simply, a block is a group of data, and multiple blocks come together to form a blockchain.
Every block in a blockchain possesses the following properties:
- Data to record on the blockchain, e.g., transaction data
- A block hash, the ID of the block generated using cryptography techniques
- The previous block’s hash is the cryptographic hash of the last block in the blockchain. It is recorded in every block to link it to the chain and improve its security
- A timestamp of when the block was created and added to the blockchain
- Proof of Work (PoW) is the amount of effort taken to derive the current block’s hash. We will explain this in depth later in the tutorial
When should you use a blockchain?
Because of their structure, blockchains are most useful when you are trying to build a system that requires decentralization on a large scale, requires data to be unchangeable once added to the system, or requires transparency and integrity of the system’s data. The most common applications of blockchain technology are in the fields of cryptocurrency and decentralized finance. Still, they can also be used in voting systems, supply chain management, and even healthcare.
Why build a blockchain with Go?
Go provides many unique features and functionalities that make it a good fit for building a blockchain. For example, Go allows you to create highly efficient and performant applications with little effort. Go is also excellent for building applications that require parallelism and concurrency (like blockchains) with its ability to spawn and manage thousands of Goroutines.
Go implements automatic garbage collection and stack management with its runtime system. Finally, it compiles applications to machine code and single binaries, supporting multiple OSs and processor architectures, and deploys easily on server infrastructure.
Building a blockchain with Go
Let’s start by creating a new Go project and importing all the necessary packages to build our blockchain. Create a file named blockchain.go
and import all the dependencies you need by saving the following code in it:
package main import ( "crypto/sha256" "encoding/json" "fmt" "strconv" "strings" "time" )
Next, we will create a custom type to represent the blocks that will make up our blockchain. Add the following code to the blockchain.go
file:
type Block struct { data map[string]interface{} hash string previousHash string timestamp time.Time pow int }
Then, we will create a custom Blockchain
type that contains our blocks. Add the following code to the blockchain.go
file:
type Blockchain struct { genesisBlock Block chain []Block difficulty int }
The genesisBlock
property represents the first block added to the blockchain. In contrast, the difficulty
property defines the minimum effort miners must undertake to mine and include a block in the blockchain.
Calculating the hash of a block
As we discussed earlier, the hash of a block is its identifier generated using cryptography. We will derive the hash for each block in our blockchain by combining and then hashing the hash of the previous block, the data of the current block, the current block’s timestamp, and PoW using the SHA256 algorithm.
Let’s create a method on our Block
type that generates a hash:
func (b Block) calculateHash() string { data, _ := json.Marshal(b.data) blockData := b.previousHash + string(data) + b.timestamp.String() + strconv.Itoa(b.pow) blockHash := sha256.Sum256([]byte(blockData)) return fmt.Sprintf("%x", blockHash) }
In the code above, we did the following:
- Converted the block’s data to JSON
- Concatenated the previous block’s
hash
, and the current block’sdata
,timestamp
, andPoW
- Hashed the earlier concatenation with the
SHA256
algorithm - Returned the base 16 hash as a
string
Mining new blocks
Mining is the process of adding a new block to the blockchain. This involves generating a block hash that starts with a desired number of zeros (the number of zeros is called the mining difficulty). This means if the mining difficulty is three, you have to generate a block hash that starts with "000"
like, "0009a1bfb506…"
.
Because we are deriving a block’s hash from its data and PoW, we need to keep changing the PoW value of the current block until we get a hash that satisfies our mining condition (starting zeros > difficulty). To implement this, we will create a mine()
method for our Block
type that repeatedly increments the PoW
value and calculates the block hash
until we get a valid one.
Add the following code to the blockchain.go
file:
func (b *Block) mine(difficulty int) { for !strings.HasPrefix(b.hash, strings.Repeat("0", difficulty)) { b.pow++ b.hash = b.calculateHash() } }
Creating the genesis block
Next, we will write a function that creates a genesis block
(the first block on the blockchain) for our blockchain and returns a new instance of the Blockchain
type. Add the following code to the blockchain.go
file:
func CreateBlockchain(difficulty int) Blockchain { genesisBlock := Block{ hash: "0", timestamp: time.Now(), } return Blockchain{ genesisBlock, []Block{genesisBlock}, difficulty, } }
Here, we set the hash of our genesis block
to "0"
. Because it is the first block in the blockchain, there is no value for the previous hash, and the data property is empty. Then, we created a new instance of the Blockchain
type and stored the genesis block
along with the blockchain’s difficulty
.
Adding new blocks to the blockchain
Now that we have implemented functionalities for our blocks to calculate their hash and mine themselves, let’s create a function that will construct a new block from its arguments and then add the new block to the blockchain. Add the following code to the blockchain.go
file:
func (b *Blockchain) addBlock(from, to string, amount float64) { blockData := map[string]interface{}{ "from": from, "to": to, "amount": amount, } lastBlock := b.chain[len(b.chain)-1] newBlock := Block{ data: blockData, previousHash: lastBlock.hash, timestamp: time.Now(), } newBlock.mine(b.difficulty) b.chain = append(b.chain, newBlock) }
Here, we created an addBlock
method to the Blockchain
type that does the following:
- Collects the details of a transaction (sender, receiver, and transfer amount)
- Creates a new block with the transaction details
- Mines the new block with the previous
block hash
, currentblock data
, and generatedPoW
- Adds the newly created block to the blockchain
Checking the validity of the blockchain
We have successfully created a blockchain that can record transactions. So, now we need functionality that checks if the blockchain is valid. A valid blockchain is one where no transactions have been tampered with. Add the following code to the blockchain.go
file:
func (b Blockchain) isValid() bool { for i := range b.chain[1:] { previousBlock := b.chain[i] currentBlock := b.chain[i+1] if currentBlock.hash != currentBlock.calculateHash() || currentBlock.previousHash != previousBlock.hash { return false } } return true }
Here, we recalculated the hash of every block, compared them with the stored hash values of the other blocks, and checked if the previous hash value of any other block is equal to the hash value of the block before it. If any of the checks fail, the blockchain has been tampered with.
Using the blockchain to make transactions
We now have a fully functional blockchain! Let’s create a main()
function to show its usage. Add the following code to the blockchain.go
file:
func main() { // create a new blockchain instance with a mining difficulty of 2 blockchain := CreateBlockchain(2) // record transactions on the blockchain for Alice, Bob, and John blockchain.addBlock("Alice", "Bob", 5) blockchain.addBlock("John", "Bob", 2) // check if the blockchain is valid; expecting true fmt.Println(blockchain.isValid()) }
Adding new nodes to the blockchain
A blockchain isn’t useful to anyone if it’s confined to just one device. So, you might be wondering how to add new nodes to the blockchain prototype you just built. Pulling this off involves creating a Peer-to-Peer (P2P) network to connect the nodes, implementing a consensus algorithm, and taking security countermeasures. However, modifying the blockchain we built to include these features is out of the scope of this tutorial.
Conclusion
In this tutorial, you learned about blockchains and Go. More specifically, you learned how blockchains work under the hood — including what blocks are and what they contain — and how to calculate a block hash, implement a consensus algorithm for mining blocks, record transactions on the blockchain, and validate the authenticity of a blockchain.
The source code of the Go blockchain is available as a GitHub Gist, and you can play with it in your browser on the Go Playground. I can’t wait to see the amazing things you build with Go, as it is a good fit for applications where efficiency and performance are a top priority. Happy coding!
Join organizations like Bitso and Coinsquare who use LogRocket to proactively monitor their Web3 apps
Client-side issues that impact users’ ability to activate and transact in your apps can drastically affect your bottom line. If you’re interested in monitoring UX issues, automatically surfacing JavaScript errors, and tracking slow network requests and component load time, try LogRocket.
LogRocket is like a DVR for web and mobile apps, recording everything that happens in your web app or site. Instead of guessing why problems happen, you can aggregate and report on key frontend performance metrics, replay user sessions along with application state, log network requests, and automatically surface all errors.
Modernize how you debug web and mobile apps — Start monitoring for free.
Sounds interesting
With the above code, I’m getting prevHash : 30 for the first block while printing the block data. It should be empty right?
I’m using the below function to print the data of the block :
func printBlockInformation(block Block) {
fmt.Printf(“\ttime: %s\n”, block.timestamp.String())
fmt.Printf(“\tprevHash: %x\n”, block.previousHash)
fmt.Printf(“\tHash: %x\n”, block.hash)
}
Can someone please help?