DEV Community

Cover image for A API client is born
Victor Hugo Avelar
Victor Hugo Avelar

Posted on

A API client is born

Episode 2 - Project foundation

So let's get going, be sure to have golang installed in your computer, if like me you're using macOS and Homebrew then you just need to execute the following command:

brew install go

The API client

The module we are going to create will showcase the top 10 links to avoid a huge display block.

For this, we will need to access the information from devto API and for this, we need code to programmatically access this information and be able to use it.

So let's start

The project directory

Open a terminal window and navigate to the place where you store your go code, thanks to the implementation of go modules it doesn't need to be the src folder inside the $GOPATH so I will navigate to ~/Code on my terminal.

We are going to create the directory in which our module is going to live and then we will go inside the recently created folder, you can accomplish this by executing the following

mkdir devto-api-go && cd devto-api-go

Initializing Go modules support

To initialize a Go module you need to execute the init command of the go mod tool, so let's do it, first I will put the "template" and then the actual execution, I like to do this because that is how my brain stores information, so sorry if it's confusing.

⚠️ This is important ⚠️ as you learn go you will
understand that some of the go tooling for using third party code
relies on git features.

So when initializing a module you should use the path to your
repository in the form of {provider}/{username}/{repository}

I like to think of it as a mock for the folder structure I would have created if using the $GOPATH.

So from now on, I will use path-to-your-repo to alias the composition described in my previous quote.

# Template for initializing a new go module once you're inside the 
# project directory
go mod init {path-to-your-repo}

Which for our project will be

# Template for initializing a new go module once you're inside the 
# project directory
go mod init

Please don't be surprised if nothing comes back, you will also get used to the fact that in go, no news are amazing news 😂

The final touch

Once our module is ready, you can create a folder called devto, there is no reason for this, it is a thing I picked, but I cannot remember how 🤔, but it will be useful in the future, I can promise that.

Finally this is how your go.mod file should look like


go 1.12

Discussion (0)