Go from the beginning - your first program

softchris

Chris Noring

Posted on June 3, 2022

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:

💖 💪 🙅 🚩
softchris
Chris Noring

Posted on June 3, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

My First Project using Golang
go My First Project using Golang

September 9, 2024

Go的基本操作(一)
go Go的基本操作(一)

February 22, 2024

Free Online Courses By Google
tutorial Free Online Courses By Google

March 2, 2023

Working with Time Data in Go
go Working with Time Data in Go

February 7, 2023