# Variables and Constants

> Declaration and initialization of variables/constants

\ <br>

## Before getting started

<br>

### Constants

* Variables that cannot change their values
  * **const** in Javascript!

#### Untype constant

* Constants without type
  * ex)

    ```go
    package main

    func main() {
     const name = "chloe"
    }
    ```

#### Type constant

* Constants with type
  * ex)

    ```go
    package main

    func main() {
      const name string = "chloe"
    }
    ```
* Of course, since it's a constant, you cannot change the value!
  * ex)

    ```go
    package main

    func main() {
      const name string = "chloe"
      name = "camila" // -> not possible
    }
    ```

<br>

### Variables

* Literally variables (values can be changed)
  * **let** in Javascript!

#### Untype variable

* Variables without type
  * ex)

    ```go
    package main

    import "fmt"

    func main() {

     var year = 2020
     fmt.Println(year)

    }
    ```

#### Type variable

* Variables with type
  * ex)

    ```go
    package main

    import "fmt"

    func main() {

     var month int = 12
     // -> month := 12 (same)

     fmt.Println(month)
    }
    ```

<br>

### Types in Go

* Go tries to figure out the type of the value you wrote
  * Because Go is a **type language**!
    * Must tell what the *"Type is!"*
      * Like Java or typescript, you need to specify the type

\ <br>

## 1. Declaring variables

<br>

### 1-1. Variable declaration method in Go

#### `var variable_name variable_type`

* You can set initial values right where you declare the variable

  ex)

  ```go
  package main

  func main() {
    var num1 int = 1000
    var num2 int = 1413

    println("Let's do addition ~ ", num1 + num2)
  }
  ```

<br>

### 1-2. `:=`

* Called Short Assignment Statement
* Variable declaration possible **without type declaration**
  * Go finds the type for you!
    * Using this shorthand seems like it can be used flexibly like python!
* Can **only be used inside functions**!!

  ex)

  ```go
  package main

  func main() {
   var a int = 100
   var b string = "Haha"
   c := 200
   print(b, a+c);
  }
  ```

<br>

\
`+`

Be careful that Go generates an error if you create a variable and **don't use it**!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chloe-codes1.gitbook.io/til/go/go-101/02_variables_and_constants.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
