Basics and Syntax of Golang
Golang (or Go) is a modern programming language developed by Google. It is known for its simplicity, high performance, and concurrency support. In this blog, we will explore the core concepts and basic syntax of Go.
Installation and Setup
To get started with Go, you need to install it from the official website. After installation, you can verify the version using the terminal:
go version
Structure of a Go Program
A basic Go program:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Code Explanation:
package main
— defines the main package of the program.import "fmt"
— imports the standard library for text output.func main()
— the main function of the program, the entry point.fmt.Println
— a function to print text to the console.
Declaring Variables
In Go, variables can be declared in multiple ways:
var name string = "Orkhan"
var age int = 26
height := 1.75 // shorthand
var
is used for explicit declaration.:=
is used for shorthand without specifying the type (type inferred automatically).
Basic Data Types
int
— integersfloat64
— floating point numbersstring
— stringsbool
— boolean type (true/false)
Control Structures
Conditional Statements
if age > 18 {
fmt.Println("Adult")
} else {
fmt.Println("Minor")
}
Loops
for i := 0; i < 5; i++ {
fmt.Println(i)
}
Functions
Defining and using functions:
func add(a int, b int) int {
return a + b
}
result := add(2, 3)
fmt.Println("Result:", result)
Working with Arrays and Slices
var arr [3]int = [3]int{1, 2, 3}
slice := []int{4, 5, 6}
array
— fixed sizeslice
— dynamic array
Conclusion
Go is a powerful yet simple language, perfect for developing high-performance applications. In this blog, we covered the basics to help you start programming in Go. In the next post, we will explore more advanced topics such as interfaces and goroutines.