Laravel to Go — Part 1 — Basics

Chris Townsend
Geek Culture
Published in
4 min readJun 16, 2021
Laravel to Go series

Switching from a predominant background in PHP to Golang has been an insightful journey, and if you are like me who hasn’t come from a strictly typed language background it can be a little daunting at first. Of course, once we were in PHP 7/8 territory we could have strict return types etc, but it isn’t quite the same, but it was good preparation to switch. I was asking myself how I move a Laravel Application to a Go project?

My Laravel to Golang series will cover Laravel concepts and patterns that can be achieved using Go. Please remember that Laravel is a framework, so it’s not like you can just pick up Go and build like you were building something in Laravel. You need to put the building blocks together and although I’ll go through some tools you can use, I won’t be using an equivalent framework in Go.

To get started we are just going to get a project up and running, and serve it. Just like if you were developing locally with Laravel and using php artisan serve

TL;DR: https://github.com/townsymush/laravel-to-go-series link to the code from this exercise

Pre reading

Before starting out on this series I presume you have used Go in some way, even if it is Hello World territory. Please read the following resources before reading further

  • Install Go — Installation guide for Go, though I recommend using something like Brew if you can
  • Tour of Go — Tour of Go will walk you through various basics and is an interactive course.
  • Effective Go — Gives you a fundamental understanding of how to use Go. It covers best practices, formatting and theory for things like interfaces in Go.

Package manager

In Laravel the default package manager is Composer. This manages the dependencies of your project, the framework and any other dependencies required for your project. Go has a dependency manager baked in called Go Modules. It has been available since 1.11 and if you used Go before this release, I have to say it was a major pain managing your dependencies. Go Modules made setting up your environment a lot easier. It's basically composer for Go but a lot faster and simpler. We also don’t have to worry about configuring things like PSR namespacing.

Go Modules

I recommend always initialising Go Modules at the beginning of a project. Create a folder for the root of your project.

Once in the folder run go mod init {project-name} This will create an empty go.mod file ready to be populated with your weird and wonderful dependencies... hopefully not too weird.

If you wanted to add a dependency in your Go project just run go get with the package name

Example: go get github.com/go-chi/chi. This will add the package to your go mod file.

If you want to update the package, just like if you ran composer update you can use the -u flag.

Example: run go get -u github.com/go-chi/chi

There are more things you can do with Go Modules so feel free to do more reading here. For now let’s move on.

Main.go

All Go applications require a main.go file. This is where you will serve your application. You could actually do everything in a really long main function, but abstraction is important here. Just like you wouldn’t do all your code in an index.php.

Laravel serves itself from index.php so let’s make the equivalent in Go. We can start by making a main.go file at the route of your project folder

package mainimport (
"fmt"
)
func main() {
fmt.Println("Hello World")
}

Things to note

  • import is like the use statement in PHP. We are naming the dependencies we are using in this file
  • fmt is an internal build in package. Hence why you don’t see something like github.com/user/fmt

the main file is where we will bootstrap our application. From here we will move into serving the application and creating a structure. Laravel follows the Model View Controller (MVC) pattern. I wouldn’t usually structure a Go project in this way, but to help keep things familiar we will follow this pattern later in the series.

Serving HTTP Server

To have a working application we need to serve our app over HTTP, just like when you’d follow the steps to install Laravel and then call php artisan serve. In Go we need to call on the net/http package

  • We have included 3 dependencies in our main.go file
  • net/http — this is for everything http related and is part of the Go internals
  • log — this is for logging, we are using it here to kill the app and log when the server dies
  • io — this is used for input and output tasks. We are just using it here to output some tests in our browser

Let’s serve our application and see what we have! In the command line at the root of your project type go run main.go. Now visit http://localhost:7777/ and see your creation.

I’m very aware this first part is very basic, but just wanted to put something in to get you started so you can continue the series.

Now that we have an application running, in part 2, I would like to guide you in splitting up your application so it represents a more familiar pattern (MVC). We will discuss routing and ways to split your application. I wouldn’t usually structure my Go application in this way, but I think in this scenario helps you get to grips coming from a Laravel background.

If you enjoyed this, please give me a clap, or even a cheeky follow.

Over and out

Chris

Update 30th April 2022 — Part 2 has been released, please go take a look

--

--

Chris Townsend
Geek Culture

I'm a Software Engineer, Mentor, Leader and Procrastinator. I've been developing for over 7 years and been a techie since I could hold a mouse