GO 101 — Go Basics (Chapter 1)

Cenk Köroğlu
4 min readDec 25, 2021
GO 101 — Go Basics (Chapter 1)

I would like to start my first post by explaining Go and the basics of Go. Go is called “simple programming language” in many sources. If you already know another programming language, and have learned and decided to develop with Go, you will understand why it is called simple. When you start writing Go, you will probably compare the concepts and writing styles of many different software languages, but my advice is to try to learn as if you are learning a software language for the first time, without comparing it to any language (without thinking that what is the equivalent of variable definitions and loops in .NET Core in Go).

Let’s jump quickly to the first topic of Chapter 1, defining variables.

Chapter 1.1 — Defining Variables

We use the var prefix when defining variables in Go.

We can use the var prefix both to define a list of variables and to define variables one by one.

If we look at the defining variables as a list;

var (
name string
surname string
age int
)

In the above sample code block, we can define variable by specifying the name of the variable and the type of the variable.

Go also allows us to define variables of the same type in a single line.

var (
name, surname string
age int
)

If we look at the defining variable one by one;

var name string
var surname string
var age int

We can define the variables one by one as in the sample code block above.

We can collect variables of the same type in a single var prefix when defining variables one by one, just like in a list definition.

var name, surname string
var age int

Chapter 1.2 — Assigning Value to Variables

We can assign values to variables both during and after declaration.

Each variable in Go has one initializer.

var (
name string = "John"
surname string = "Doe"
age int = 24
)

If we assign a value to the variable during definition, we do not have to specify the type of the variable. Go will accept the type of value you give to the variable as the variable type.

var (
name = "John"
surname = "Doe"
age = 24
)

In this example, when we look at the runtime types of name, surname, and age variables, we will see that the name and surname variables are “string” and the age variable is “int”.

We can do something similar to the example above by defining the variables on the same line.

var (
name, surname, age = "John", "Doe", 24
)

After defining variables, we can use = (equal sign) to assign values to variables.

var (
name string
surname string
age int
)
name = "John"
surname = "Doe"
age = 24

We can not assign value by using = (equal sign) to a variable that we do not define with var!

In this point, a short variable definition method comes into our lives in the form of := without using var.

The point we need to pay attention to here is that we can only use:= inside functions, we have to use var prefix when defining variables outside of functions.

func main() {
name := "John"
surname := "Doe"
age := 24
}

In Go, a variable can have any data type, even a function.

func main() {
myFunction := func() {
//some actions
}
myFunction()
}

Chapter 1.3 — Constants

Actually, we define constants like variables, one of the two differences is that we use the prefix const instead of the var prefix. The second difference is that the type of constants can only be string, boolean, number and character. Constants cannot be changed at runtime, they are defined once and remain with their defined value.

Also, we cannot define constants and assign values using :=.

const (
Pi = 3.14
)
func main() {
const ApiKey = "EAz82fcHrre6FVscgu40TkNTSUViXlGS"
}

Chapter 1.4 — Printing Variables and Constants

We can print the variables and constants we define with the print and println functions that are built-in functions in Go. But since we will do various formatting in our examples, we will use the package called fmt that is built-in package in Go.

You can find the functions and usage examples in the fmt package at https://pkg.go.dev/fmt.

func main() {
const ApiKey = "EAz82fcHrre6FVscgu40TkNTSUViXlGS"
fmt.Println(ApiKey)
}

ThePrintln function will output to the console and go new line. The output of this example will be as follows;

EAz82fcHrre6FVscgu40TkNTSUViXlGS

We use the Printf function when we want to output to the console by formatting our variables.

func main() {
var name = "John"
surname := "Doe"
age := 24
fmt.Printf("%s %s is %d years old", name, surname, age)
}

The console output of this example is;

John Doe is 24 years old

In this lesson, we learnt how we can define variables and constants in Go language, and different definitions that you can use according to your coding style. We also learnt in this section how to assign values to the variables we defined and how to output them to the console. In the next chapter, I will talk about package and import logic, functions and pointers in Go. See you later :)

--

--