Lesson 3Storing with Variables

Lesson 2 presented a lot of topics. In this lesson, we'll begin to dig deeper into some of those topics. Specifically, this lesson provides an overview of creating variables in Go, including static and dynamic types as well as global and local variables.

VARIABLES

Variables in Go are identifiers. For example, we might need to store the email address of a client but also need to make sure it is valid. In this case, we can create a variable named email that stores the value of the email. The email address can be assigned to the email variable.

The variable references a memory address where the actual value assigned to the variable is stored. When the compiler needs the value of the variable, it uses the named memory address to retrieve it.

Whenever you create a variable, you must include its type, either by defining the type explicitly or by assigning a value with an identifiable type in the same statement. The basic syntax to create a variable looks like this:

var identifier type [= value]

The name of the variable ...

Get Job Ready Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.