Go Basic: Understanding Data Types, Values, Constants, Variables and Scopes in Go

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 or float64.
  • Booleans, which represent true or false. They’re declared with the boolkeyword.
  • Strings, a sequence of characters defined between double quotes “”.

For example:

1
2
3
4
var x int = 5  
var y float64 = 9.5
var z bool = true
var s string = "Hello, Golang"

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 intfloat64boolstring, and many others.

Here’s how you can use them:

1
2
3
4
var i int = 10  
var f float64 = 230.5
var b bool = false
var str string = "Hello, world!"

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
2
3
4
5
const (  
a = "Hello"
b = true
c = 5
)

In the above example, abc 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
2
3
var x int = 1 // Declaring with type and initial value  
var y int // Declaring with type, assigns "zero value" (0 for ints)
z := 1 // Short variable declaration, type inferred by Go

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 (xyz, 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:

  1. 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
2
3
4
func hello() {  
var message string = "Hello, Golang"
fmt.Println(message) // This will work
}

fmt.Println(message) // This will not work since ‘message’ is out of scope

  1. 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
2
3
4
5
var message string = "Hello, Golang"  
func hello() {
fmt.Println(message) // This will work
}
fmt.Println(message) // This will work

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.

15 lessons help you understand Go better — Golang Basic Skill Tree Reentrant Locks You Should Know in Golang

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×