Go: from zero to your own blog Lesson 4 of 50
Variables and types in Go: string, int, float64 and bool
The fourth lesson of the Go course. What a variable is and why the language insists on types: string, int, float64 and bool, declaring with := and var, zero values, and why a type is chosen once and never changes. We also read the "declared and not used" error you will meet on day one, and how to name things readably.
Why this matters
Your server answers everyone the same thing. To answer differently — the reader’s name, an article’s title, a view count — the program has to keep values somewhere. A place that holds a value is called a variable.
And Go always asks what kind of value it is: text, a number, a yes-or-no. That is called a type, and it is how the language catches a whole class of mistakes before the program even runs.
The whole thing at once
A new folder, go mod init sabaq03, a file main.go:
package main
import "fmt"
func main() {
title := "Salem, alem!"
views := 0
rating := 4.5
published := true
views = views + 1
fmt.Println(title)
fmt.Println("Views:", views)
fmt.Println("Rating:", rating)
fmt.Println("Published:", published)
}
Before you run it, and without looking below, say out loud what it will print. Then run it and compare: the difference is exactly where you do not know the language yet.
go run . prints:
Salem, alem!
Views: 1
Rating: 4.5
Published: true
Four variables, and that is almost the article card of your future blog.
Taking it apart
A variable is a name for a value
A labelled box. views is not the number 0; it is a name with 0 behind it right now. A line later there is a 1 there.
Picture it. A labelled box on a shelf. The word “views” written on it never changes; what is inside changes whenever you like. You say “fetch me the views”, not “fetch me the zero”.
:= declares and fills in one go
Read title := "Salem, alem!" as “make a title and put this string in it”.
We never wrote the type anywhere. Go looked at what was on the right and worked it out — that is called type inference. Text in quotes on the right, so title is a string.
Picture it.
:=is fetching a new box and writing a label on it.=is putting something into a box that is already labelled and on the shelf. There is no reason to label the same box twice.
The := sign works only the first time a name appears. After that it is a plain =:
views = views + 1
Read that as an order, not an equation: take what is in views, add one, put it back. In mathematics the line would be false; in programming it is ordinary.
Four types that will do for now
- string — text: a title, a name, an address. Written in double quotes.
- int — a whole number: views, a count of comments.
- float64 — a number with a fractional part: a rating of 4.5, a height, a temperature. The separator is a dot. Money is not stored this way:
float64counts approximately and the small change drifts apart over time. For money you take a whole number of the smallest units — tiyn — or a dedicated decimal type. - bool — only
trueorfalse: published or not.
The names of these built-in types are lower-case. For types you define yourself the case of the first letter means something else — whether the name is visible outside its package; we get to that in the lesson on structs.
Picture it. A type is the shape of the box. A fridge will not go into a shoebox, and that is a good thing: the mismatch shows up in the warehouse rather than in the buyer’s kitchen.
var — when there is no value yet
Sometimes you need the variable now and will learn what goes in it later:
var author string
var count int
var ok bool
Here you have to name the type yourself — there is nothing to infer it from. Go puts the zero value of that type in: "" for a string, 0 for a number, false for a bool.
That matters more than it looks. In Go there is no variable with “who knows what” inside: it always holds something. A whole class of bugs that other languages trip over simply cannot occur.
Picture it. An empty glass is an empty glass, not “who knows what”. You can pick it up, hold it to the light, pour into it. In some languages what stands there instead is “unknown whether there is a glass at all”, and that is what people trip over.
const — what never changes
It happens the other way round too: the value is known in advance and must not change. A limit on the length of a title, the address of the site, the names of states. For that there is const:
const maxTitle = 80
const site = "shanraq.org"
const (
draft = iota
review
published
)
func main() {
fmt.Println("title limit:", maxTitle)
fmt.Println("site:", site)
fmt.Println("states:", draft, review, published)
}
title limit: 80
site: shanraq.org
states: 0 1 2
There is one difference from a variable, and it is a hard one. Try to change a constant and the program will not build:
cannot assign to maxTitle (neither addressable nor a map index expression)
The iota inside the brackets is a counter that starts at zero and goes up by one on every line. It is for when there are several states and what matters is not the numbers themselves but that they differ: draft, review, published in the code instead of 0, 1, 2, which nobody will be able to read a month later.
One more rule: the value of a constant has to be known at build time, not while the program runs. Hence the second error, worth seeing in advance:
var author = "Daulet"
const site = author
author (variable of type string) is not constant
A variable can be changed, so its value is unknown until the program starts — which makes it unfit for a constant.
Picture it. A stamp rather than a pencil mark. A mark gets corrected; a stamp does not — it was cut once, and it comes out the same everywhere after that.
The rule is simple: if a value is written into the code by hand and must not change, write const. The number 80, scattered over four places in the code, will one day be changed in three of them.
A type is chosen once and does not change
views was declared a whole number, and text will not go into it:
views = "a lot"
Go refuses to build the program and says roughly this:
cannot use "a lot" (untyped string constant) as int value in assignment
That is not the language being fussy. A mistake caught at build time costs you a minute. The same mistake found by a reader on a running blog costs incomparably more.
The error you will meet today
Declare a variable and do not use it, and Go refuses to build:
declared and not used: author
Almost any other language would keep quiet. Go treats an unused variable as litter and will not let it through.
There are two cures: use it, or remove it. It annoys you for exactly one week, and then you start to appreciate it.
How to name variables
Latin letters, no spaces. Several words are joined up, each one after the first capitalised: articleTitle, viewsCount. This is called camelCase, and it is what Go does everywhere.
Picture it. It is how you label folders in a cabinet. Not “f1” but “Lease agreements 2026” — so that a year later you open the cabinet and find it first time.
A name should say what is inside. You will not remember t in a week; you will always understand title.
The lesson map
Say it in your own words
Answer out loud or on paper without looking. The answers are at the end of the lesson.
- How does
:=differ from=? - You wrote
var price float64and put nothing in it. What is inprice? - Why does Go refuse to build a program over an unused variable, when it bothers nobody?
Exercise
Required. Build the card of your future article: four variables with your own values, printed to the terminal.
Optional.
- Try putting a string into an
intvariable and read the whole message. Meeting that error on purpose once means recognising it later. - Declare a variable and do not use it. Check that Go will not build the program.
- Replace one
:=declaration with avarand an explicit type. The result should be identical.
If it did not work.
go: cannot find main moduleorgo.mod file not found— you are in a folder with nogo.mod: rungo mod init sabaq03in it.undefined: fmt— theimport "fmt"line is missing; the editor writes it for you on save if you installed the Go extension. Anddeclared and not usedis not a breakage but the conversation from the section above.
Where this goes in your blog
These four variables are the fields of your future article. In the lesson on structs we gather them into one Article type so they travel together, and in the lesson on CRUD they go off to a database and come back.
Answers
Show the answers
:=makes a new name and gives it a value at once, with Go working out the type.=puts a value into a name that already exists. You can repeat:=with the same name — but only when at least one name on the left is new: the old one then simply takes a new value.- Zero. Every type in Go has a zero value:
0for numbers,""for strings,falsefor bool. There is no “who knows what is inside” variable here, as there is in some languages. - Because an unused variable is nearly always the trace of a mistake: you worked something out and forgot to apply it, or mistyped the name below. Go would rather stop you now than let that reach a reader.
Sources
If you have found a mistake or a typo in this article, tell us about it
Comments (0)
Log in to leave a comment →
No comments yet. Be the first.