Introduction
Over the holiday break, I have been trying to learn Go. At first, I thought Go was kind of weird, but now I get it a bit more and it’s quite fun to learn. Here are some things I learned.
Trying Go
You can try Go by visiting https://play.golang.org in your web browser. It is a simple editor where you can enter Go code and run it on Go’s servers. The results of your code are displayed in the browser. Just change some text and then click on Run. The results of the program will be displayed below:
You can also install Go on your computer, which is what I have done. I then use Visual Studio Code as my text editor when writing programs. Here is what went on in this program.
// This is a comment
package main // A package is a
// collection of code that
// does similar things.
import "fmt" // Import the fmt package.
// You import only the packages
// your program needs
func main() { // Special function that
// gets run first when
// your program starts
fmt.Println("Hello, everyone!") // This displays
// "Hello, everyone!"
// to your terminal
// by calling the
// Println function
// from the "fmt"
// package
}
When Go programs run, it looks for the function called main and runs that first. These are the three main sections of a Go program:
-
The package clause
-
The imports section
-
Your code
Function calls
You can call a function by typing the functions name and a pair of parentheses and zero or more arguments:
fmt.Println()
fmt.Println ("Hello", "World")
In the first example, we call the Println function that is part of the fmt (format) package, with no argument. Nothing will be printed.
In the second example, the Println function is called with two arguments.
You have to import the package before you can access the packages functions.
Question: What would you have to enter in the following blanks for the output to say Merry Christmas! ?
package main
import _________
func ________()
Answer:
package main
import "fmt"
func main() {
fmt.Println("Merry Christmas!")
}
Go Data Types
Data type specifies the size and type of variable values. Some of Go’s data types are:
-
rune: represents single characters.
-
bool: represents a boolean value and is either true or false
-
Numeric: represents integer types, floating point values, and complex types
-
string: represents a string value
-
Runes represent Unicode characters. Unicode is actually the collection of all possible characters present in the whole world. If you pass a rune to fmt.Println, it will output the numeric code and not the original character. Runes are written with a single quotation mark (‘ ‘).
-
Boolean values can be either true or false.
-
Numeric types include int, float32, float64, complex64, etc. An int holds a whole number. A float64 is a floating-point number and hold fractional numbers.
-
Strings are surrounded by double quotes ( “ “ ).
Variables
Variables are containers for storing values. There are two ways to declare a variable in Go:
Use the var keyword followed by the desired name of the variable and the type of values the variable will hold:
-
var variableName type
Examples:
var myQuantity int
var height, weight float64 // this declares multiple variables with the same type at one time
var userName string
var dayofMonth int = 29 // if you know the variable’s value, you can declare the variable and assign value
2. variableName := value
Examples
myQuantity := 51
height, weight := 5.8, 135
userName := “jdoe”
If you already know what the initial value of a variable is going to be when you declare it, it is more typical to use this short variable declaration. You do not need to declare the variables type because the type of the value assigned to the variable become the type of that variable.
If you declare a variable without assigning it a value, the variable will contain the zero value for its type.
Here is an example of not assigning a value to variables:
package main
import ("fmt")
func main() {
var myString string
var myInt int
var myBool bool
var myFloat float64
fmt.Println(myString)
fmt.Println(myInt)
fmt.Println(myBool)
fmt.Println(myFloat)
}
When you run this program, you will see the following:
fritzie@underacloud GoPractice % go run intro.go
0
false
0
Variable Names
There are several rules for Go variable names. They include:
Go variable naming rules:
-
Name must start with a letter or an underscore character (_)
-
You can use short names for variables, like a or b, or long names
-
Variable names cannot start with a number
-
Variable names can only contain alpha-numeric characters and underscores (
a-z, A-Z
,0-9
, and_
) -
Case sensitivity – age is different than AGE
-
Variable names cannot contain spaces
-
You cannot use Go keywords as variables
-
If variables consist of multiple works, each word after the first should be capitalized. This style is called camel case. An example is thisIsMyLongVariable.
-
If a variable name begins with a capital letter (MyExample), it is considered exported and is accessible from packages outside of the current package.
Conclusion
Today I covered function calls, data types, and variables. If you are interested in learning the basics of Go, check out https://go.dev/tour/list. Next time I will cover conditions and loops and start writing some real programs.