First, you need to download and install Go from the official website https://golang.org/dl/
. After installation, set up the GOPATH
environment variable. The GOPATH
is a directory where your Go projects and dependencies will be stored.
Open your terminal and run the following command to check if Go is installed correctly:
go version
If installed correctly, it will display the installed Go version.
In Go, you can declare variables in two ways: using the var
keyword or the short variable declaration operator :=
.
package main
import "fmt"
func main() {
// Using var keyword
var num int = 10
var name string = "John"
// Short variable declaration
age := 25
fmt.Printf("Name: %s, Age: %d, Number: %d\n", name, age, num)
}
Go has several basic data types, including integers (int
, int8
, int16
, etc.), floating - point numbers (float32
, float64
), booleans (bool
), and strings (string
).
Go has traditional control structures like if - else
, for
, and switch
.
package main
import "fmt"
func main() {
num := 15
// if - else
if num > 10 {
fmt.Println("Number is greater than 10")
} else {
fmt.Println("Number is less than or equal to 10")
}
// for loop
for i := 0; i < 5; i++ {
fmt.Println(i)
}
// switch statement
day := 3
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
default:
fmt.Println("Other day")
}
}
Functions in Go are defined using the func
keyword.
package main
import "fmt"
// Function with parameters and return value
func add(a int, b int) int {
return a + b
}
func main() {
result := add(5, 3)
fmt.Println("Sum:", result)
}
Methods are functions that are associated with a particular type (usually a struct).
package main
import "fmt"
type Rectangle struct {
width float64
height float64
}
// Method on Rectangle struct
func (r Rectangle) area() float64 {
return r.width * r.height
}
func main() {
rect := Rectangle{width: 5, height: 3}
fmt.Println("Area of rectangle:", rect.area())
}
Structs are used to group related data together.
package main
import "fmt"
type Person struct {
name string
age int
}
func main() {
p := Person{name: "Alice", age: 30}
fmt.Printf("Name: %s, Age: %d\n", p.name, p.age)
}
Interfaces in Go are a set of method signatures. A type implements an interface if it provides definitions for all the methods in the interface.
package main
import "fmt"
// Shape interface
type Shape interface {
area() float64
}
type Circle struct {
radius float64
}
func (c Circle) area() float64 {
return 3.14 * c.radius * c.radius
}
func printArea(s Shape) {
fmt.Println("Area:", s.area())
}
func main() {
circle := Circle{radius: 5}
printArea(circle)
}
Go uses packages to organize code. You can import packages using the import
keyword.
package main
import (
"fmt"
"math"
)
func main() {
num := 16
sqrt := math.Sqrt(float64(num))
fmt.Println("Square root:", sqrt)
}
Go modules are used to manage dependencies. To initialize a new module in your project directory, run the following command:
go mod init <module - name>
Go will create a go.mod
file that tracks your project’s dependencies.
Goroutines are lightweight threads of execution in Go.
package main
import (
"fmt"
"time"
)
func printNumbers() {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(i)
}
}
func main() {
go printNumbers()
time.Sleep(1 * time.Second)
fmt.Println("Main function finished")
}
Channels are used to communicate between goroutines.
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum
}
func main() {
s := []int{1, 2, 3, 4, 5}
c := make(chan int)
go sum(s, c)
result := <-c
fmt.Println("Sum:", result)
}
In this blog, we have covered the fundamental aspects of Golang syntax and structure, from basic variable declarations to advanced concurrency features. By understanding these concepts, you are well on your way to becoming proficient in Go. Remember to practice writing code regularly and follow best practices to write efficient and maintainable Go applications.
https://golang.org/ref/spec
https://golang.org/doc/effective_go
https://gobyexample.com/