Week 1: Golang doesn't have While loop

Yagya GoelYagya Goel
2 min read

This week, I learned about array slices, maps, structs (classes in Golang), if-else statements, switch-case, loops, functions, methods, and the defer keyword.

Golang and why it matters. Go, or as its easily google-able… | by James  O'Toole | Medium

Golang is a pretty interesting language with its opinionated syntax that keeps the code looking clean. However, Golang can also be confusing at times.

Some of the problems I faced are:

First letter Case matter a lot
Yes you heard it right , Making the first letter of a variable or method capital or lowercase can make it public or private.

For is Go's "while",
the for loop is used in place of a while loop. Actually, there is no while loop in Go; we use for as a while loop.

NO Try Catch
Yes, there is no try-catch in Golang. Golang handles errors in a different way. It treats errors as regular values, which helps us understand the flow more easily.

Also, talking about errors, the panic function also confused me. Panic helps us stop the execution of the program when there are fatal errors.

No Inheritance
The concept of structs, which are like classes in Golang, can be confusing for someone coming from Java. There is no inheritance in Golang, so there are no super or parent classes. This can easily confuse newcomers.
The idea behind no inheritance is to increase flexibility, simplicity, and code reusability.
Golang uses something known as composition.
In this approach, a struct can have other structs as objects.

Declaration Difference
In Golang, declarations are a mix of JavaScript and Java. We use the var keyword to declare a variable along with its type, similar to Java. Additionally, the lexer allows us to use the := syntax to declare and initialize variables without explicitly defining var and its type.

Braces {} need to be on the same line as the func, if, else, etc., otherwise, it will cause an error. This happens because the lexer adds a ; at the end of every line. Adding a ; at the end of func greeter(); closes the function or if else, causing an error.

These are some of the problems I faced, but Hitesh Sir helped me understand these concepts better. Please make sure to check out his Golang series.
Here is the Link

See you next time!

Yagya Goel
Youtube
Linkedin
Pinterest

1
Subscribe to my newsletter

Read articles from Yagya Goel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yagya Goel
Yagya Goel

Hi, I'm Yagya Goel, a passionate full-stack developer with a strong interest in DevOps, backend development, and occasionally diving into the frontend world. I enjoy exploring new technologies and sharing my knowledge through weekly blogs. My journey involves working on various projects, experimenting with innovative tools, and continually improving my skills. Join me as I navigate the tech landscape, bringing insights, tutorials, and experiences to help others on their own tech journeys.