Variables

In Go, we have several data types that are used to store different kinds of information:

  • Integer: Represents whole numbers without a decimal component.
  • String: A sequence of characters enclosed within quotes.
  • Float: Represents numbers that contain a decimal point.
  • Boolean: Represents one of two values, true or false.
  • List: An ordered collection of items, here using an interface{} to allow any type.
  • Dictionary: A collection of key-value pairs where keys are strings and values are any type.

It's good practice to name your variable relevant to its purpose, and use camelCase for naming conventions. Variable names cannot start with a number, include special characters other than '_', or contain spaces.

Here are some more variable examples:

var (
    itemCost        int     = 1     // The cost of an item, which will be 1 dollar
    supplyRemaining int64   = 2500000000 // The number of items in inventory that are remaining
    nameCTO         string  = "John Woods" // Name of a CTO
    valueOfAQuarter float64 = 0.25  // The value of a quarter in dollars
)

There are several arithmetic operators available in Go, and they are as follows:

a := 10
b := 3
addition := a + b
subtraction := a - b
multiplication := a * b
division := myFloat / float64(b)
floorDivision := a / b
modulus := a % b
exponentiation := math.Pow(float64(a), float64(b))

Similar to integers and floats, you can also use arithmetic operators on strings:

result := myText + " World!"
fmt.Println("Concatenated String:", result)
repeatedString := strings.Repeat(myText, 3)
fmt.Println("Repeated String:", repeatedString)

We won't dive into Lists, Dictionaries, or Tuples just yet, but I would like to mention, and this is something you'll see in practice later— that lists and dictionaries are mutable, whereas tuples are immutable. When you hear mutable, think "can be changed", where mutable means it can be changed and immutable means it cannot be changed.

Example:

my_tuple = (1, 2)
print(my_tuple[0])
my_tuple[0] = 2
print(my_tuple)
# ERROR
#    my_tuple[0] = 2
#    ~~~~~~~~^^^
# TypeError: 'tuple' object does not support item assignment

A more advanced concept to start early with, but another thing we will review later:

Examples of mutable and immutable parameters on an asset on Algorand

Immutable Parameters:

These parameters can only be specified when an asset is created.

  • Creator: The address of the account that created the asset.
  • AssetName: The name of the asset.
  • UnitName: The unit name of the asset.
  • Total: The total number of units of the asset.
  • Decimals: The number of digits to use after the decimal point when displaying the asset.
  • DefaultFrozen: Whether the asset is frozen by default.
  • URL: A URL where more information about the asset can be retrieved.
  • MetaDataHash: A commitment to some unspecified asset metadata.
Mutable Parameters:

These parameters can be changed after the asset is created.

  • Manager: The address of the account that can change the asset's mutable parameters.
  • Reserve: The address of the account that holds the asset reserve. (Cannot be changed if initially not declared)
  • Freeze: The address of the account that can freeze or unfreeze user asset holdings. (Cannot be changed if initially not declared)
  • Clawback: The address of the account that can revoke user asset holdings and send them to other addresses. (Cannot be changed if initially not declared)
*Note: Exception for various ARC types like ARC19 and ARC69 that use the reserve address and/or note field to point to metadata*

Quiz

Question 1

Which of the following correctly declares a variable in Go?





Question 2

Which of the following variable names is valid in Go?





Question 3

What will be the result of the following code?

a := 10
b := 3
addition := a + b
fmt.Println(addition)




Question 4

How do you perform exponentiation in Go?





Code Editor