DEV Community

Rahat Chowdhury
Rahat Chowdhury

Posted on • Edited on • Originally published at rahatcodes.com

2

Declaring Variables in Go

I've recently taken up learning Go on the side to broaden my knowledge as a developer and dive into some backend a little more. Since I'm learning this language from scratch there are lots of things I'm going to be sharing. This post starts off a little small with declaring variables in Go.

Declaring variables is probably one of the first things you learn in any language after a Hello World! (We're skipping that here). Let's look at one way of doing a variable declaration:

var name string = "Rahat"
Enter fullscreen mode Exit fullscreen mode

Ok so...yay var looks like there isn't a let and const war in this language...that was a JavaScript joke. Go is a statically typed language so if you're like me and used to dynamic languages like JavaScript, Python, or Ruby you might say at first, oh this is different. In Go we keep the type of the variable consistent, so if name is a string I can't later replace it with some numbers, it's always got to be a string!

That's great and all but Go gives us a nicer way of making a declaration:

name := "Rahat"
Enter fullscreen mode Exit fullscreen mode

Looked a little weird to me at first but this is great, Go will infer that you want the variable name to be a string and you won't have to declare the type every time. One thing to note is that this syntax would only be used when declaring. If I want to change my name, later on, I would do something like this:

name := "Rahat"
name = "Goku"
Enter fullscreen mode Exit fullscreen mode

Once I change my name to Goku I can just use an equal sign since I'm re-assigning the value of the name variable.

I hope you enjoyed the post, it would be great to see if others are learning Go as well, always looking for accountability partners! Feel free to send over a tweet or dm @rahatcodes.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay