Building a Blockchain with Go - Go Modules and a Basic Blockchain - Part 1

in #utopian-io5 years ago

golang.jpg

Repository

https://github.com/golang/go

What Will I Learn?

  • You will learn how to use the Go module system
  • You will learn how to create a Hash from a slice of bytes
  • You will learn how to implement the basic framework of a Blockchain

Requirements

System Requirements:

Operating System:

  • FreeBSD 10.3 or later
  • Linux 2.6.23 or later with glibc
  • macOS 10.10 or later
  • Windows 7, Server 2008R2 or later

Required Knowledge

  • A little understanding of the Go programming language
  • Go installed on your computer
  • A text editor or IDE like Gogland (VS Code used)

Resources for Go and this Project:

Difficulty

  • Basic

Description

In this Golang video tutorial, we take our first steps into building a full featured blockchain with the Go programming language. We also take a look at the new Go module system which was added to the language as of Go version 1.11 and how it simplifies package management in Go.

Modules in Golang

When Go was first released, the language was built with a somewhat unorthodox system called the GOPATH. To develop Go applications, a user needed to define a directory on their computer as your GOPATH. This GOPATH environment variable tells the compiler where to find directories that contain Go source code outside of the installation directory. The old module system is derived off of this concept and there was no first party package manager included in the Go tool chain.

package-go.png

With the release of Go version 1.11, this module system was reworked so that the GOPATH is no longer a requirement. A new command called go mod was added to the tool chain. This command allows the user to define a go module inside of any directory on their computer. This module system also enables package management with version control. The above image shows a go.sum file which is similar to a yarn.lock or package-lock.json file.

Laying the framework for a Blockchain

In general terms, a blockchain can be defined as a distributed peer to peer database. The blockchain itself is like a linked list made of hashes. Each block contains its own hash and a hash that points at the proceeding block in the chain. The block also contains the data which is being stored on the chain. This data is used to derive the block's hash through a proof of x hashing algorithm.

blocks.png

In this example, we implement this basic generalization with Go. A block type is defined with a hash, a previous hash reference and a data field. The hash for each block is derived using a SHA256 algorithm which doesn't require much work. This algorithm acts as a placeholder for the hashcash proof of work algorithm that will be implemented in future videos. When the data changes, the block's hash changes which makes it easy to verify that the data is corrupted.

The Source Code for this video may be found here: https://github.com/tensor-programming/golang-blockchain

Video Tutorial

Proof of Work Done

https://github.com/tensor-programming

Sort:  

Hello @tensor

Great to see you back contributing to the video-tutorials.

Again, thank you for creating this video tutorial. I enjoy going through it with your nicely paced steps in this introduction to Go Module system.

I also love the fact that the font sizes are big for learners to follow.

Keep up the excellent work and I look forward to your next tutorial in this series of building a blockchain with Go Module.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you Rosa,

I am glad that I finally have some time on my hands and can actually make this series. Been meaning to do it for a long time now.

Thank you for moderating my Contribution.

Tensor

Thank you for your review, @rosatravels!

So far this week you've reviewed 1 contributions. Keep up the good work!





This post has been voted on by the SteemSTEM curation team and voting trail in collaboration with @curie.

If you appreciate the work we are doing then consider voting both projects for witness by selecting stem.witness and curie!

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

Thanks guys. I love being a part of the SteemSTEM community. I am glad a few of you reached out to me so early in my steemit career.

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

As smart contract developer and #programming tag lover, I've upvoted your nice post! Cheer up!

Thank you, hope you enjoy the rest of the series.

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hi, got a couple of questions:

  • Is there any shorthand like go mod init github:tensor/gochain or just go mod init tensor/gochain?
  • Is rsc.io is some kind of registry (like npm) that was recommended by golang community?

There is no shorthand as of yet for the module system, though if you already have github initialized, it will automatically fill in the module name and match it with the name of your remote repository. So, I could have typed in go mod init and it would have added the github.com/tensor-programming/go-blockchain part by itself.

rsc.io is just a set of packages that I was using. The registry for go is called go-doc: https://godoc.org/

Hi, @tensor!

You just got a 8.75% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Congratulations! Your post has been selected as a daily Steemit truffle! It is listed on rank 19 of all contributions awarded today. You can find the TOP DAILY TRUFFLE PICKS HERE.

I upvoted your contribution because to my mind your post is at least 12 SBD worth and should receive 117 votes. It's now up to the lovely Steemit community to make this come true.

I am TrufflePig, an Artificial Intelligence Bot that helps minnows and content curators using Machine Learning. If you are curious how I select content, you can find an explanation here!

Have a nice day and sincerely yours,
trufflepig
TrufflePig

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.032
BTC 64555.14
ETH 3086.03
USDT 1.00
SBD 3.85