Demystifying Go (Golang), Google's Programming Language: Constants and Variables

Demystifying Go (Golang), Google's Programming Language: Constants and Variables

Introduction

If you've already survived exploring Go's package structure, the main() method, and the glorious fmt package to print things on the screen, congratulations! Now it's time to take another step toward enlightenment. Today, we'll dive into the wonderful world of constants and variables in Go. Get ready, because Go offers several ways to declare them, each with its own charm and specific usefulness.

Oh, and a quick note: Go is a strongly typed language. Yes, you read that right. Unlike JavaScript, variables and constants in Go always have a defined type. Whether you declare it explicitly or rely on the compiler's inference, Go doesn’t leave it to chance.

Declaring Types Explicitly

Let's start with the basics: declaring variables and constants with explicitly defined types. Here’s an example:


As you can see, constants must always have a value, while variables can be left without one. In that case, they’ll assume what's called a zero value (don’t worry, we’ll talk about zero values in upcoming posts).


Note: In this example, we used the native reflect package. It has a function called TypeOf, which, as the name suggests, tells you the type of a value. Simple and straightforward, no mysteries here.

Declaring with Type Inference

Go is also flexible enough to infer the type of variables and constants from the assigned value. This saves you some keystrokes and keeps your code cleaner. Check out how it works:


In this form, you must provide a value for both variables and constants. If you forget, the compiler will gently remind you—with a syntax error.


Declaring Variables without the "var" Keyword

Now comes the trick every Go developer loves. Instead of using the traditional var keyword, you can declare variables more quickly and elegantly using the := operator. It's one of the most popular techniques in the Go world. Take a look:


Just a heads-up: this technique works exclusively for variables. Constants still need the good old const keyword. There's no escaping that formality.

Construction Blocks

If you're a fan of organization (or just want to look more elegant), you can declare multiple variables or constants at once, in a single block. This is what we call construction blocks:


Looks like a good idea, right? And it is!

One-Line Declaration

If you think your code can get even more concise, why not declare multiple variables on a single line? Here's how you can do that:


And of course, you can initialize them too:


The only catch is that, in this case, all the variables must be of the same type. After all, you can't be that daring without paying a price.

One-line declaration with Type Inference

And now, to finish with flair: the combination of one-line declaration with type inference. This is the cherry on top, the pinnacle of simplicity and efficiency. Here, you can declare multiple variables of different types, all in a single line. Want to see how this wonderful madness works?


Magic, right? Who said Go can't be fun?

Conclusion

Go may seem minimalistic at first glance, but it's this very simplicity that makes it such a powerful and efficient language. The flexibility in declaring variables and constants reflects Go's philosophy: practicality and performance without sacrificing clarity. Whether you're declaring types explicitly or relying on type inference, the key is to keep your code readable and elegant. Now that you know how to handle constants and variables, you're ready to tackle bigger challenges in the Go world. So, get coding and keep pushing forward!

Idalio Pessoa

Senior Ux Designer | Product Designer | UX/UI Designer | UI/UX Designer | Figma | Design System |

5 个月

I love how Go encourages practicality & performance without sacrificing clarity. The simplicity in declaring variables & constants is not only efficient but also user-friendly, making it easier for devs to focus on what matters most.

回复
Guilherme Duailibe

Senior Fullstack Software Engineer | Python | React | PHP | 20+ years of experience

5 个月

Useful tips

Rodrigo Dias

Senior React Native Developer

5 个月

Useful tips. Thanks for sharing.

Juan Soares

Fullstack Software Engineer | React | NodeJS | TypeScript | JavaScript | AWS | DevOps | TDD | 3x AWS Certified

5 个月

I agree, great content!!

Rodrigo Tenório

Senior Software Engineer | Java | Spring | AWS

5 个月

Good to know

要查看或添加评论,请登录

Vagner Nascimento的更多文章

社区洞察