DEV Community

Cover image for 30 Days of Go: Day 1

Posted on • Updated on • Originally published at

30 Days of Go: Day 1


I have decided to take part in a challenge to learn Golang in the next 30 days. I found a Go hackathon and thought this is the perfect opportunity because I have wanted to learn it for a while now. I have decided to split this learning up in a couple of ways. The idea is to build an application for the hackathon. My current app idea is a citizen science API. This API will allow users to upload photos of animals such as birds or insects.

I have split my learnings into the following categories:

  • Weeks 1 & 2: Learn the syntax and basics of Golang and do the gophercises
  • Week 3 & 4: Build out the API

Golang syntax

This information is referenced Golang documentation and especially Go by example.


  • Golang's variables are explicitly declared like C#, Java
  • Golang allows multiple variables to be declared in one line
  • If the type has not been specified go will infer the types from initialized values
var a = "Hello world"
var age int = 8
// When the type has been declared it is used for both variables
var name, game string = "Scott Pilgrim", "vs The world"
// This is allowed because go will infer the types from initialization
var name, age = "Knives Chau", 18
Enter fullscreen mode Exit fullscreen mode
  • Variables that are declared but not initialized will be zero-valued
  • All declared but not initialized variables must have a type
var one int
// one = 0
var decision bool
// decision = false
var word string
// word = ''
Enter fullscreen mode Exit fullscreen mode
  • := is shorthand for declaring and initializing a variable e.g
num := 50
Enter fullscreen mode Exit fullscreen mode


  • Constants in Go work the same as in other languages. A const can be a string, character, bool, or numerical type.
const test string = "Steven Stills"
Enter fullscreen mode Exit fullscreen mode
  • Numerical constants perform arithmetic with arbitrary precision
  • A numerical constant has no type until it is given one from a function or is cast to a type

Flow Control


  • Go only has for loops and they can be initialized in the following ways:
i := 0
for i <= 4 {
    i = i + 1

for j := 4; j <=8; j++ {
Enter fullscreen mode Exit fullscreen mode
  • infinite loops have no conditions and can be stopped with a break statement
for {
    fmt.Println("The infinite sadness")
Enter fullscreen mode Exit fullscreen mode
  • the continue keyword can be used to go to the next iteration of a loop
for i :=3; i <= 33; i++ {
    if i % 3 == 0 {
    fmt.Println("Love evans")
Enter fullscreen mode Exit fullscreen mode

If else

  • If else blocks are like most other programming languages
  • Parenthesis are not needed in the if statement but braces are
if x == 3 {
    fmt.Println("Young Neil")
} else if x > 5 {
} else {
Enter fullscreen mode Exit fullscreen mode
  • Go's if block can have a statement before the condition. Any variable declared in the statement can be used in all the branches of the if block.
if x := 4; x < 0 {
} else if x > 1 {
} else {
    fmt.Println("Do something else")
Enter fullscreen mode Exit fullscreen mode
  • Go has no ternary if statements like javascript x == 2? "Yes": "No"

Switch statements

  • A switch statement in Go is like other programming languages
  • The default case is optional
  • Cases can have multiple expressions
  • a switch can have no expressions which will make it function like an if statement
switch i {
case 1:
case 2:
case 3, 4, 5:
    fmt.Println("Default is optional")

// Swtich with no condition
t := 5
switch {
case t < 4:
    fmt.Println("Less than 4")
Enter fullscreen mode Exit fullscreen mode
  • A type switch compares types instead of values
  • Use this switch to discover an interface's type
  • Using .(type) does not work outside a type switch
myType := func(i interface{}){
    switch t := i.(type){
    case bool:
 fmt.Println("Im boolean")
    case int:
 fmt.Println("Im integer")
 fmt.Println("Im anything else")
Enter fullscreen mode Exit fullscreen mode

This was a fruitful first day and there's more to come.

Discussion (9)

supportic profile image

The .(type) assumption check is very useful but yet too early to stumble upon when you just started imo. :D
What you will also see alot is giving back a result + err from a function.
result, err := function(x,y) and in addition to that the often used err != nil check

if err != nil {
  fmt.Println("oops something went wrong")
Enter fullscreen mode Exit fullscreen mode

I would take a look on go project structure or code style guide best practises.

Welcome to GO :)

kmukabe profile image
Kacha Author

Thanks for that feedback @supportic, I was having trouble seeing how I'd use the type switch at this point. I'll keep it on the back burner for now, I'm planning to take a look at function syntax and data structures today so thanks for the links.

Thanks for the welcome as well glad to be here 😁

supportic profile image

Pretend using the universal any type interface{} in a function parameter. Say you work with JSON values and you want to get the type of them in order to process them properly.
A type switch may help you because you know the structure/types you get and therefore can safely assume which types you get. (see type switch )
In the example you also see a very handy print function.
fmt.Printf("%v\n", res) eventually you know the printf function from C/C++. However the %v let's you print any type you want. Additionally with classes you might want to use %+v to also see the properties.

Thread Thread
kmukabe profile image
Kacha Author

Ahhh interesting, I see this is quite helpful. I'll need to use this in a little toy app to really cement the concept

clavinjune profile image
Clavin June

don't forget, Go has fallthrough keyword in switch statement

kmukabe profile image
Kacha Author

I'll update the post to add this in there

mrtung profile image
Mr Tùng


bellatrix profile image
Sakshi Jain

Hi! I am also taking part in same hackathon and learning flutter and go. Hope we'll have fun in learning journey...

kmukabe profile image
Kacha Author

Hi @bellatrix glad to have another hackathon lover here. How's your go journey um going 😅?