Basics
Notes on the GO Programming language
Updated: 03 September 2023
Workspace
Go is very specific in the way we set up our folders by making use of a specific structure for our workspace folders
A workspace should have a bin
and src
folder in which our project code will reside, we typically use something like the following
We need to set the GOPATH
environment variable to the folder in which we want our workspace to be
In my case I’m using C:\repos\go
Next make the necessary directories
Install Package
Install a package with the go get
command, for example the aws
package:
Hello World
Make a new project directory with a file called main.go
with the following content
Go has a main
function that needs to be created for every package and it will be run automatically. We also need to import fmt
to print content to the screen. Also note that Go strings must have double quotes "hello"
or we will get an error
We can run this with the following command from the project dir
Build and Run
We can build binaries for an application with go install
which can then be executed from the terminal
Build and run the package with the following command from the project directory
And then from the bin
directory
Variables and Data Types
Go has the following data types
- string
- bool
- int int8 int16 int32 int64
- uint uint8 uint16 uint32 uint64
- byte (uint8)
- rune (int32)
- float32 float64
- complex64 complex128
There are a few different ways to create variables, note that if we create a variable and do not use it we will get an error
Var
Type Inference
Get Type
We can make use of the following function to get the type of a variable
Constants
We can define constants with the const
keyword
Global Variables
We can declare global variables by defining them outside of the main function
Shorthand Method
Inside of a function we can declare variables using the assignment operator with the following
Multiple Assignments
We can do multiple assignments as follows
Packages
Importing Packages
We can import multiple packages by declaring each package in the import
statement on a different line
Creating Packages
Create a new folder and in it you can define the package name and some functions in it
And then import the package by referring to its path in the import function
Functions
Functions are defined with the func
keyword, the function name, inputs, and return types and values
Arrays and Slices
In Go arrays are fixed length, and a slice is an array without a fixed size
Arrays
Arrays are fixed in size and can be defined with
Or with initial values
Slices
A slice is essentially an array that does not have a fixed size
We can also make slices by using the same notation as other languages
Conditionals
Conditionals do not require parenthesis, however they can be used
If Else
Swtich/Case
Loops
There are two methods for building for loops
Maps
Maps are key-value pairs and can be defined and accessed with
We can delete a value from a map with the delete
function
We can also declare a map with initial values
Range
Range is used for looping through values
If we are not using the i
variable, we can use an _
to receive any inputs that we will not use
Pointers
A pointer allows us to point to the memory address of a specific value, we can get the pointer for a value with the &
sign
If we wan to get back from the pointer to the actual value we can do that with the *
operator
We can modify the value of a pointer
The reason to use pointers can be more efficient when passing values
Closures
We can define anonymous functions to declare anonymous functions that can be used as closures
Structs
Structs are like classes
Structs can contain values and functions, of which we can have value reveivers and pointer receivers. Value receivers just do calculations, Pointer Receivers modify data
Interfaces
Web
To work with HTTP requests we can use thenet/http
package which allows us to define a function to handle a specific route