Golang — Syntax Review

Harith Javed Bakhrani
5 min readAug 13, 2019

--

I am writing this to serve as a review guide. This guide is for you if:

  • You have a strong experience in another programming language (especially language like C), and want to quickly catch up on Golang’s syntax.
  • You have already learned Golang or are in the process of learning it and you are now looking to quickly revise Golang syntax.

This guide covers the following:

  1. Writing a basic “Hello World” program.
  2. Packages.
  3. Variables and types.
  4. Functions.

Hello, World!

Print “Hello, World!” using Golang

Below is a brief explanation of the line numbers in the above program:

Line #1: Declares that this file is the main package.

Line #3: Imports fmt package.

Line #5: Starts the main function

Line #6: Uses Println function from the fmt package.

Line #7: Denotes the end of the main function.

Packages

Creating your own Packages

You can create your own packages inside the src folder:

Creating packages in golang

In the above picture, I have created a package named greet. Here are some things to note while creating a package:

  1. They should be within the src folder and can be nested within other folders.
  2. To make a variable or a function exportable (this means you would like to use the variable or the function in another package), the name of the variable or function MUST begin with a capital letter.

Consider using the below convention if you would be using a version control system, like Git:

github.com/username/project-name/

Using 3rd party Packages

Go team has made this example package that we can use as an example.

Using external Golang packages

To start using stringutil package that is located at github.com/golang/example/stringutil, we should first run:

go get github.com/golang/example/stringutil

The above command would fetch the package and insert it in our src directory, keeping the directory structure as it is, github.com directory being the parent directory.

Variables & Types

Go is statically typed, meaning that Go knows the types of values even at compile time.

Variables

Variables must be declared before using them. When declaring variables, you must state the type of value you want to store in the variable. The convention is as follows var name type. The example below declares a variable n and states that it will be storing integer values only.

var n int    // we are declaring variable n to be of type int
n = 2 // we cannot store any other type
var a, b int // we can also declare multiple variables

If you are declaring and initializing a variable at the same time, then there is no need to state the type as Golang would automatically know the type:

var i = 2
var s = "This is a string"

There is even a shorter notation of declaring and initializing variables (can be used inside functions only):

i := 1
s := "Short way of initialising string"
a, b := 2, 4

You can only perform math operations and comparisons on variables of the same type. You can, however, convert one type to another:

a := 2
b := 3.5
fmt.Println(a + b) // this would throw an error
fmt.Println(float64(a) + b) // convert before performing operation

A variable’s scope is limited to the block in which it is defined.

BONUS: CamelCase convention is used when naming variables.

Basic Types

boolstringint  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8rune // alias for int32
// represents a Unicode code point
float32 float64complex64 complex128

BONUS: What type to use for an integer value? Use int unless you have a specific reason to use a sized or unsigned integer type.

Functions

Convention for declaring a basic function is:

func func-name() {    // you can replace func-name with any name
// code goes here
}

Functions can optionally take in one parameter or even multiple parameters:

func myFunction(number int) {
// do something with number variable + rest of code
}
func myOtherFunction(s string, number int) {
// do something with the parameters + rest of code
}
func thirdFunction(a, b float64) {
// do something with the a and b + rest of code
}
  • You must declare a type for all the parameters.
  • Take a look at thirdFunction (in the above code), we have declared two parameters and stated their types at the end, this means that both the parameters would have the same type.

Functions can also optionally return values:

// returns a string
func greet() string {
return "hello, world!"
}
// returns a string and an integer
func vals() (string, int) {
return "Multiple return values", 7
}
// takes in a number and returns its square
func square(number int) int {
return number * number
}
func add(a float64, b float64) (sum float64) {
return a + b
}

func subtract(a, b float64) (difference float64) {
difference = a - b
return
}
  • You must specify return value type
  • Like vals() function, you can also return multiple values from a function. This is usually used to return an error, if any. When calling the function, you can ignore the second value by assigning it to an underscore:
    s, _ = vals(). However, when you decide to ignore an error, be sure of what you are doing.
  • You have the option to name the return values, take a look at func add and func subtract. It can be used as documentation.
  • In func subtract, we don’t need to explicitly specify to return the variable difference because we have named the return value.
  • If a function is designed to return a value, then the last statement of that function must be return.

I hope this served as a benefit to you. If you have any kind of suggestion or you would like me to expound more on any of the above topics, please feel free to leave a comment.

Have a lovely Go ride!

Thank you.

📝 Read this story later in Journal.

👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.

--

--

Harith Javed Bakhrani
Harith Javed Bakhrani

Written by Harith Javed Bakhrani

Muslim DevOps Engineer ready to learn and bring to life new and better ways of automating deployments and keeping them alive!

No responses yet