Video:

Go gRPC Service Project Setup

April 22, 2021

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

Let’s kick this course off by creating a new Go project under which we’ll be building our gRPC service.

Project Structure

We’ll want to structure our project in a way that it’s easily testible, extendible as well as understandable. We’ll start off by creating a cmd directory which will house the main entry points for our application. Within this directory we’ll have a server/ directory and under that our main.go file.

This is the same approach we generally take with all of our Go projects as it allows us to be flexible should we need to add additional entrypoints such as a command-line interface or a GUI.

go-grpc-services-course
- cmd/
- - server/
- - - main.go

Next, let’s create an internal directory that will contain things like our service, the transport and the database implementations.

go-grpc-services-course
- cmd/
- - server/
- - - main.go
- internal/

Within this we can then create the placeholder directories for the packages we need:


go-grpc-services-course
- cmd/
- - server/
- - - main.go
- internal/
- - db/
- - rocket/
- - transport/

Finally, we’ll want to add a Readme.md that will contain some information about our service. We can leave this fairly empty for now, but as we progress through the course we can add information around how we do things like startup our application locally, or run our test suites.

Our main.go Entrypoint

Our main.go file within cmd/server/ should be empty as it stands, let’s change that now and create a main func and a Run func that we’ll call from our main func:

package main

import "fmt"

func Run() error {
  fmt.Println("Rocket Service Starting...")
  return nil
}

func main() {
  if err := Run(); err != nil {
    log.Fatal(err)
  }
}

Now this approach of splitting out the setup code into a separate Run function may seem overly verbose as it stands, but when we come to test our application, this approach helps us out tremendously.

Repository Setup

Finally, we’ll want to set up our GitHub repository so that we can commit our code. I’ve already got a repository setup for this project, but if you are following along on your own machine then you should take the time to initialise your repository now:

$ git init
$ git remote add origin <your repo>
$ git add -A
$ git commit -m "initial commit"
$ git push origin master

Conclusion

Awesome, so in this video, we have successfully built the foundations for our gRPC service and pushed it up to a Github repository!