Create Go service the easy way
By: Petr Jahoda
Tags: Golang, Go, Docker, Tutorial, Programming

Start writing the code

Add two lines below package main , that contains information about this service, we are creating. Service name and it’s description.

No we will add an external module, that allows us to run this program as a service. Add another line of code, just below the package main. This import line will have (for now) a red squiggle line below itself. This means, that this module is not imported.

Import the module

Place your cursor on this red squiggly line and hit ALT+ENTER (or OPTION+ENTER if you are a mac user). Select Sync dependencies … Goland will download this module from internet and combine it with your code.

But that red squiggle line doesn’t dissapeared. Why is that?

The answer is simple. We imported the module, but we are not using it now. The red squiggle line tells us this exact information: we are not using this imported module. So let’s use it.

Use the module

Add new line of code type program struct{} , just above the main function. This line of code contains a structure. Structure that holds your service. That red squiggly line is still there.

To get rid of it, we need to add some code into that main() function. Code, that will almost create and start you service. Code below.

Explain the module

I think, we need to pause here and go through the code.

Line starting with serviceConfig adds your name and description to the service. When you run this service in the future you will know what’s the name of the service in Windows, Linux or MacOS service properties.

You can add an information about a version, for example like this: const serviceName = “Medium service, version 2021.1.2 .

Goland will automatically add new import line for fmt as this is internal Go package, not an external one.

Lines below will almost create — service.New() — and — s.Run() — your service.

Implement module methods

You see, we have two new red squiggle lines. The first one is in s, err := service.New(prg, serviceConfig) and the second one in err = s.Run() . To get rid of them, place your cursor in that prg word, hit ALT+ENTER and select Implement missing methods.

Goland will create those two missing methods. Code below is what you’ll see. Two new methods: Start() and Stop() .

Update the code in those two methods with code below, so we can see — when the service is running — what the service is doing. Those lines fmt.Println() will print on the screen, that your service has started and/or ended. But there is another red squiggly line under go p.run() .

Adding production code

Again: put your cursor on that go p.run() line, hit ALT+ENTER and choose Create method run. This will create a new method, that will hold our production code.

To make a simple production code simulation, update that method, so it looks like code below. This code will print an information, that your service is running, every second. When you add that time.Sleep() method, Goland will automatically import time package for you.

Test the code

We arrived at the end. Your code should look like below. Now press your right mouse button in that main.go file and select Run go build main.go. Goland will run your code.

When you start your code, let it run for a while and then stop it (by pressing red stop square button). You’ll see something like this.

Medium service started

Service is running

Service is running

Service is running

Service is running

Service is running

Medium service stopped Process finished with exit code 0

Congratulations.

Your service is starting, running and stopping the right way. And as a bonus you can install this small program as a service under your system of choice.

But, for now, it will still stop immediately. To make it stop after production code is done — after that 1 second simulation work is done — please check improving the service article.

If you want to know up-front, how to create an executable, jump directly into this article.