Before diving into values, constants, and variables, it’s important to know about data types.
full lessons here👇:
https://programmerscareer.com/golang-basic-skill/
Generated by AI, there may be errors, for reference only
In Go, each variable has an associated data type. This data type determines the characteristics of the variable, such as its size, and the nature of operations that can be applied on it.
The basic data types in Go include:
- Integers, positive or negative whole numbers with no decimal point. They can be declared as
int
in Go which automatically assigns the appropriate bit size based on your system’s CPU architecture (32 or 64). - Floating point numbers, or “floats” for short. They represent real numbers with decimal points and can be declared as
float32
orfloat64
. - Booleans, which represent
true
orfalse
. They’re declared with thebool
keyword. - Strings, a sequence of characters defined between double quotes “”.
For example:
1 | var x int = 5 |
In each declaration, we’re specifying that a particular variable is of a particular type.
Topic: Intro to Values
Values are the fundamental data that our programs will manipulate. Each value belongs to a specific type and, in Go, values can be of type int
, float64
, bool
, string
, and many others.
Here’s how you can use them:
1 | var i int = 10 |
In Go, we can’t mix different types in the same expression. If you try to compile fmt.Println("string" + 2)
, Go will throw an error.
Topic: Go Constants
A constant is a simple, immutable value. We declare constants in Go with the const
keyword:
const pi float64 = 3.1416
Once you declare a constant, you cannot change its value. So, if you try to compile pi = 1.2
, Go will throw an error saying you cannot assign to pi
.
Constants can be character, string, boolean, or numeric values.
1 | const ( |
In the above example, a
, b
, c
are constants.
Topic: Variables in Go
Variables are declared in Go with the var
keyword. Go is statically typed, which means when you declare a variable to be of a certain type, you can only assign values of that type to the variable.
Variables in Go can be declared in multiple ways:
1 | var x int = 1 // Declaring with type and initial value |
Go also supports multiple variable declarations:
1 | var a, b, c int = 1, 2, 3 |
The :=
operator is a short declaration to declare and initialize a variable.
For example, in place of var f string = "Hello, world"
, we could just write f := "Hello, world"
Remember, :=
can only be used inside a function. For variable declarations outside functions, you must use the var
keyword.
In each case, a variable of the specified type is created, and you can get or set its value using the corresponding identifier (x
, y
, z
, etc.).
Topic: Scopes in Go
In Go, both variables and constants have a certain scope, which is the region of the code where they can be accessed.
There are mainly two types of scopes:
- Local Scope: If a variable or constant is defined inside a function, then its scope is limited within that function. This means that it can only be accessed within that function and not outside it.
1 | func hello() { |
fmt.Println(message) // This will not work since ‘message’ is out of scope
- Global Scope: If a variable or a constant is defined outside of all functions, then it can be accessed from any part of the code, including within functions.
1 | var message string = "Hello, Golang" |
Remember, the names of global variables and constants should be unique throughout the package.
Summary
So, we have covered a lot of ground today! We touched on the basics of Go, including simple data types, variable and constant declaration, and the scope of those variables and constants. Remember that practice makes perfect! Try creating your own Go programs manipulating different types of variables and constants.
中文文章: https://programmerscareer.com/zh-cn/go-basic-01/
Author: Wesley Wei – Twitter Wesley Wei – Medium
Note: If you choose to repost or use this article, please cite the original source.
Comments