Go Essentials: Mastering Variables, Data Types, and Keywords"๐Ÿ’ฏ

ยท

2 min read

Go Essentials: Mastering Variables, Data Types, and Keywords"๐Ÿ’ฏ

#90DaysOfDevOps

Day-11: Variables, Data Types and Keywords

Variables in Go

Variables in Go are declared using the var keyword. The variable name is followed by the type of the variable and the value of the variable is assigned after the equals sign (=). For example:

var myVariable int
myVariable = 10

In this example, the variable myVariable is declared as an int and is assigned the value of 10.

Variables can also be declared with an initial value. For example:

var myVariable2 string = "Hello, world!"

In this example, the variable myVariable2 is declared as a string and is assigned the value of "Hello, world!".

Data Types: Go has several built-in data types, including:

  • Numeric Types: Go provides various numeric types such as int (signed integers), float32 and float64 (floating-point numbers), byte (unsigned 8-bit integer), and more.

  • Boolean Type: The bool type represents boolean values, which can be either true or false.

  • Strings: Strings are a sequence of characters enclosed in double quotes ("). Go uses Unicode characters, allowing you to work with international text effortlessly.

  • Arrays and Slices: Arrays are fixed-size collections of elements, while slices are dynamic and resizable. They are declared using square brackets ([]) and support operations like indexing, slicing, and appending.

  • Structs: Structs allow you to create custom data types by combining multiple fields of different types into a single entity. They are declared using the struct keyword.

Keywords in Go

Go has a number of keywords that are used to control the flow of the program. Some of the most common keywords include:

  • if

  • else

  • for

  • switch

  • case

  • default

  • break

  • continue

  • return

Keywords are reserved words that have special meaning in the Go language. They cannot be used as variable names or as identifiers for other language constructs.

Did you find this article valuable?

Support Jatin Chourasia by becoming a sponsor. Any amount is appreciated!

ย