How to easily initialize a local variable for an if conditional statement in Golang or Go?
Tags: go
Published: 2022-08-13 18:58:14

---
  title: How to easily initialize a local variable for an if conditional statement in Golang or Go?
  published: true
  tags: go
  date: Sun Aug 14 2022 00:28:14 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/easily-initialize-local-variable-for-if-conditional-statement-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-d4c20cc54dbe607440b85fc91dd8acea.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/easily-initialize-local-variable-for-if-conditional-statement-golang)
  
To easily initialize a local variable for an if conditional statement in Golang, you can write the variable initialization code after the keyword `if` followed by the `;` symbol (semi-colon) and then the conditional statement.

### TL;DR

```golang
package main

import "fmt"

func main() {
    // a simple if conditional statement
    // and a local variable called `greeting`
    // with the value of `Hey`
    if greeting := "Hey"; 3 < 5 {
        fmt.Printf(greeting)
        fmt.Printf(" Yes, it is true!")
    }


    // using the `greeting` variable outside the
    // if conditional statement won't work
    fmt.Printf(greeting); // ❌ undefined: greeting. Go build failed.
}
```

For example, let's say we have an if conditional statement that checks if the value `3` is less than the value `5` and if it's true (obviously) should print the text `Yes, it is true!` to the terminal.

It will look like this,

```golang
package main

import "fmt"

func main() {
    // a simple if conditional statement
    if 3 < 5 {
        fmt.Printf("Yes, it is true!")
    }
}
```

Now to easily initialize a local variable for the if conditional block, we can specify the variable initialization after the `if` keyword and end the variable initialization with the `;` symbol (semi-colon).

Let's make a local variable called `greeting` with the value of `Hey` and then print it to the terminal.

It can be done like this,

```golang
package main

import "fmt"

func main() {
    // a simple if conditional statement
    // and a local variable called `greeting`
    // with the value of `Hey`
    if greeting := "Hey"; 3 < 5 {
        fmt.Printf(greeting)
        fmt.Printf(" Yes, it is true!")
    }
}
```

Now if you run the program, you can see an output like this,

```bash
Hey Yes, it is true!
```

Finally, to prove that this is only available in the if conditional block we specified. Let's also try to use the `greeting` variable outside the if conditional statement.

It can be done like this,

```golang
package main

import "fmt"

func main() {
    // a simple if conditional statement
    // and a local variable called `greeting`
    // with the value of `Hey`
    if greeting := "Hey"; 3 < 5 {
        fmt.Printf(greeting)
        fmt.Printf(" Yes, it is true!")
    }


    // using the `greeting` variable outside the
    // if conditional statement won't work
    fmt.Printf(greeting); // ❌ undefined: greeting. Go build failed.
}
```

As soon as you run the program the Go compiler will show you an error saying `undefined: greeting. Go build failed.` which proves the variable is declared locally to the if conditional statement.

We have successfully initialized a local variable for an if conditional statement in Golang/Go. Yay 🥳!

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

That's all 😃!

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

---