DEV Community

Cover image for Go from the beginning - your first program
Chris Noring for ITNEXT

Posted on

Go from the beginning - your first program

this is the first part of series on Go, the programming language, I hope it's helpful

The language is called Go but is sometimes known as Golang as the first website for it was golang.org.

Go was created in 2009 by Robert Griesemer, Rob Pike and Ken Thompson. It's hard to estimate the number of Go developers but it's somewhere between 1.1 and 2.7 million, quite a sizeable amount. More than 2500 companies are using Go including, Google, Pinterest and Uber. So you see, used by a lot of folks by big companies.

Why was Go created?

As is often the case, a programming language is created to deal with the shortcomings of other languages. In this case, the creators wanted this new language to have the following capabilities:

  • Static typing and run-time efficiency from C.
  • Readability from JavaScript and Python.
  • High-performance networking and multi-processing.

It seems the creators agreed on disliking C++ :)

What is it used for though?

Here's some areas where you are likely to find a Go being used:

  • Cloud based and server-side apps
  • DevOp, automation
  • Command line tools
  • AI and data science

References

There's many great resources out there for learning the Go programming language like:

Features

So what features makes Go compelling? Well, there are some features really worth mentioning that I personally appreciate:

  • Static typing, I like my types :)
  • Package system. You can consume and create your own packages. Go to pkg.go.dev to read more on what packages there are.
  • Command line tools, there's a set of executables that are installed when you install Go. With these executables, you can run, build, install packages, run tests and much more.
  • Standard library. Go has a powerful standard library that will help you with most things you might need. You can read more about what's in the standard library here.
  • Built-in testing. Having a testing library that just works out of the box is something you shouldn't take for granted.
  • Concurrency. Go is great at handling concurrency. It uses concepts like goroutines and channels.
  • Garbage collection. You can read more about that here. I like when I don't have to deal with that myself and just focusing on solving problems.

Install Go

Ok then, hope you are intrigued at this point and just want to see some code? Of course you are :)

Make sure you've followed the instructions for installing Go on your machine.

https://go.dev/doc/install

A Go program

Here's what a first program can look like:

package main

import "fmt"

func main() {
 fmt.Println("hello")
}
Enter fullscreen mode Exit fullscreen mode

The program in detail

  • package main, the entry point module needs to have this instruction.
  • import "fmt", fmt is standard package for input and output.
  • func main, entry point function, where your program starts.

Commands

Now that you have a program, there's two things you might want to do:

  • Run it, to see if it compiles and runs.
  • Create executable, an executable is no longer Go code but like any executable program on your machine.

Run your app

To run your app, type go run <file>.go, for example:

go run main.go
Enter fullscreen mode Exit fullscreen mode

this produces an output looking like so:

hello
Enter fullscreen mode Exit fullscreen mode

Build your app

To produce an executable, run go build <file>.go, for example:

go build main.go
Enter fullscreen mode Exit fullscreen mode

It produces an executable, on MacOS and Linux that's a file with -X as permission, on Windows, it's an .exe file.

Congrats, you've created your first Go application.

Summary

In this article, you learned about the programming language Go, some features it has and how to write your first program.

Learn more

There are many great resources out there for learning the Go programming language like:

Top comments (1)

Collapse
 
jonasbn profile image
Jonas Brømsø

Thanks for a very detailled introduction, looking forward to future articles