Basics of Go


Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

Check out a free preview of the full Basics of Go course

The "Factories" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano showcases patterns for creating factory functions that generate instances of structs or interfaces. These factory functions encapsulate the creation logic and offer a convenient way to construct instances of complex or customizable types.


Transcript from the "Factories" Lesson

>> Again, can you set a constructor? No, not really, not really, we don't have constructors, but what we do have is a design pattern to create a factory. How's that? What's a factory? A function, and we use a prefix of New, capital n, because we wanna make this public exporter, NewInstructor.

And we receive, I don't know, whatever we wanna receive, like, I don't know, the name and the lastname, name as a string, lastname as a string. And we return what? An instructor. It's a factory that you can do on Java. It's like a static method on other languages, C# or Java.

We don't have a static methods here. We don't have the concept of a class to apply in a static method. In this case, it's not a method, it is just a function. And we basically return here a new instructor with the FirstName: name, and the LastName: lastname. Not a big deal, but you will see this a couple of times.

Instead of creating constructors, you create a factoring function. And the design pattern is to start with the word New, so you know it's a way to create a new something, makes sense? So then from main, instead of creating the instructor like this, I can create another instructor, I can use Kyle Simpson, Kyle.

So Kyle is data.NewInstructor, "Kyle", "Simpson". So then I can print max and I can print Kyle. That's how you use the factor. It's just a function, it's an exported function from the package. Some people, some developers are creating a data package in general. Some developers are creating one package for per model.

So then the package can be called instructor. If that's the case, then you can use directly something like instructor.New. So you don't need to specify the type of element, the structure you want, okay? So you can say structure.New. By that case, you need a different package per model, per piece of information that you are saving in your app.

Does it make sense? It's not complicated, right, but this is the base of your data structure. So I'm going to create one more structure now for a course, so one which will be pretty similar. So it's a course.go package data. And we're going to create a type, Course, of type structure, that's going to have an Id.

Now, we know that we must use capital I, in case you want to make that public. Maybe that one can be private, a name, slug, that's the URL key. If it's legacy, bool. What else? Duration of type, something, we can create our own time for duration, and they create a type here, Duration float32.

By the way, should I create another Go file for that type? I don't know. Again, when I'm doing this, I'm doing semantics. We can say every time we say Duration, it's expressed in hours or in minutes. Duration Duration. And then it can into an instructor, Instructor Instructor. This is the type, this is the property name.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now