How to declare and initialize variables in Golang or Go?
Tags: go
Published: 2022-08-13 18:58:09

---
  title: How to declare and initialize variables in Golang or Go?
  published: true
  tags: go
  date: Sun Aug 14 2022 00:28:09 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/declare-and-initialize-variables-golang-go
  cover_image: https://melvingeorge.me/_next/static/images/main-ac0bef8e370d8e219d6d148f0b6aa9cd.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/declare-and-initialize-variables-golang-go)
  
To declare and initialize variables in Golang or Go, there are 2 ways to do it:

- [Declare and initialize the variables at the same time using the `:=` operator - The short way](#declare-and-initialize-at-same-time)
- [Declare variable first and initialize it later - The long way](#declare-varaiable-first-initialize-later)

### <a name="declare-and-initialize-at-same-time"></a> Declare and initialize the variables at the same time using the `:=` operator - The short way

To declare and initialize the variable at the same, we can write the name of the variable followed by the `:=` operator and then the value for the variable.

Golang compiler will automatically infer or analyze the type using the value assigned.

It can be done like this,

```golang
package main

// Method:
// Declare and initialize the variables at the
// same time using the `:=` operator - The short way

import "fmt"

func main() {
    // declare and initialize the
    // varaible using the `:=` operator
    name := "John Doe"

    // log to the console
    fmt.Print(name)
}
```

See the above code live in [The Go Playground](https://go.dev/play/p/Y-4TUUxodXa).

We have successfully declared and initialized the variable at the same time. Yay 🥳!

### <a name="declare-varaiable-first-initialize-later"></a> Declare variable first and initialize it later

To declare the variable first we can use the `var` keyword followed by the name of the variable and then its type. And to initialize the variable with a value you can use the variable name followed by the `=` operator (assignment operator symbol) and then the value you need to assign to the variable.

### TL;DR

```golang
package main

// Method:
// Declare variable first and initialize it later - The long way

import "fmt"

func main() {
    // declare variable and its type
    // using the `var` keyowrd
    var name string

    // assign a string value to
    // it using the `=` operator
    name = "John Doe"

    // log to the console
    fmt.Print(name)
}
```

For example, let's say we need to make a variable called `name` with the type of `string`.

It can be done like this,

```golang
package main

func main()  {
    // declare variable and its type
    // using the `var` keyowrd
    var name string
}
```

Now to initialize it with a value let's use the `=` operator (assignment operator) and then type the value it needs to hold.

It can be done like this,

```golang
package main

// Method:
// Declare variable first and initialize it later - The long way

import "fmt"

func main() {
    // declare variable and its type
    // using the `var` keyowrd
    var name string

    // assign a string value to
    // it using the `=` operator
    name = "John Doe"

    // log to the console
    fmt.Print(name)
}
```

See the above code live in [The Go Playground](https://go.dev/play/p/01W4sDYwPSw).

We have successfully declared and then initialized the variable in Golang 😃.

That's all 😃!

### Feel free to share if you found this helpful 😃.

---