Variables and Constants

Declaration and initialization of variables/constants

Before getting started

Constants

  • Variables that cannot change their values

    • const in Javascript!

Untype constant

  • Constants without type

    • ex)

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

Type constant

  • Constants with type

    • ex)

  • Of course, since it's a constant, you cannot change the value!

    • ex)

Variables

  • Literally variables (values can be changed)

    • let in Javascript!

Untype variable

  • Variables without type

    • ex)

Type variable

  • Variables with type

    • ex)

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

1. Declaring variables

1-1. Variable declaration method in Go

var variable_name variable_type

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

    ex)

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)

+

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

Last updated