Let’s order a pizza in Go — Part 1

An introduction to GORM library for Go.

Luis Masuelli
May 12 · 2 min read


Building a database application in Go[lang] is somewhat different to other languages (like Java, Ruby, or Python). This is mainly related to the fact that Go is a native language and thus the type system is somewhat different to what the aforementioned languages have to offer: Go’s is a static one.

Given that, making an ORM could sometimes be different, have some caveats, and may make you prefer to fallback to plain SQL libraries.

While a fallback to plain SQL is quite understandable under certain conditions, ORM can ease our way when those conditions do not bother us, and that’s why most of us prefer ORMs overall.

Now, if you are using Beego, you already have that problem solved. However, since (at least, in my opinion) Beego is an opinionated piece of crap, I prefer the idea of using iris for the web and gorm (in particular, this article stands for version v1.9.8) and, at the same time, this article serves its purpose also for database applications that are not web-based.

Connecting to a database

Before we start, we need to have a database server of our choice (MySQL, PostgreSQL, and even individual SQLite databases are supported dialects in GORM), and also we have to setup a database.

For this example, I’m creating my database pizzas in my local server. Since this is just a sample — and also it will help me demonstrating the simplicity of use — I’m creating a quite simple file you can also create (provided you installed gorm, of course, either in your $GOPATH or in your module requirements if using GO111MODULES):

package mainimport (
_ "github.com/jinzhu/gorm/dialects/mysql"
func Connect() (*gorm.DB, error) {
return gorm.Open("username:password@/pizzas?charset=utf8&parseTime=True&loc=Local")
func main() {
if db, err := Connect(); err != nil {
fmt.Printf("Dude! I could not connect to the database. This happened: %s. Please fix everything and try again", err)
} else {
// Remember: defer causes the sentence
// to be executed at the end.
defer db.Close()
fmt.Println("Database connection was successful. Enjoy.")
// Now, it is up to you and your evil
// desire.

Perhaps you noticed there is no host:port pair between @ and /. That means the host is localhost, and the port is the default (in mysql: 3306).

Considering the trailing querystring parameters are optional, I’d at least leave charset=utf8 as a de facto mandatory to be used. Aside from that, our MySQL connection is ready.

I will now assume you are using this stub, and thus your db connection will be available appropriately.

So far, our pizza store is open.

Go to part 2

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade