I'm writing about the basics of Go including variables, control flow, and more.
Let's dig in!
What is Go?
Go is a programming language produced by Google. With a simple grammar, it's easy to learn and well-suited for team development. Another characteristic is its speed. It's also a language that's growing in popularity - used by everyone from startups, where development speed is important, to large-scale system development.
Printing Hello World! in Go
The words written inside the () of println() will be displayed.
package main
func main() {
println("Hello World!")
}Save it as main.go
The output will be:
Hello World!
Note: Strings need to be enclosed in double quotes ", or there will be an error.
This defines the package:
package mainThis is the function definition (defines the main function):
func main() {
println("Hello World!")
}Writing Comments
Anything between /* and */ will become a block comment, and using // at the start of a line will make a single-line comment.
package main
func main() {
/*
Block comments
*/
println("Hello World!")
//println("this will not be displayed")
}The output will be:
Hello World!
Using Integers
Unlike strings, integers do not have to be put in double quotes. We can use them to do calculations such as addition + and subtraction -.
package main
func main() {
println(7)
println(7+3)
println(6-5)
}The output will be:
7
10
1
Doing Calculations
We can use operators such as * for multiplication, / for division, and % to calculate the remainder after division.
package main
func main() {
println(7*3)
println(8/2)
println(9%2)
}The output will be:
21
4
1
Combining Strings
We can perform operations on more than just numbers. The + operator is used to add numbers, but when used on strings, it combines them into a single string. Combining strings is known as string concatenation.
package main
func main() {
println("Hello " + "World")
}The two strings Hello and World are concatenated by the + operator to form a single string. The output will be:
Hello World
To Be Continued...
