Golang is a compiled language, which means that the source code is translated into machine - code before execution. This results in faster execution times compared to interpreted languages like Python.
In Golang, every variable has a static type. This provides better type safety and can catch many errors at compile - time.
Golang has an automatic garbage collector, which manages memory allocation and deallocation. Programmers don’t have to manually free memory, reducing the risk of memory leaks.
One of the most powerful features of Golang is its built - in support for concurrency. Goroutines and channels make it easy to write concurrent programs that can efficiently utilize multiple CPU cores.
C:\Go
) to the PATH
environment variable..bashrc
or .zshrc
):export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
package main
import "fmt"
func main() {
// Declare a variable with explicit type
var age int = 25
fmt.Println(age)
// Declare a variable with type inference
name := "John"
fmt.Println(name)
// Constants
const pi = 3.14
fmt.Println(pi)
}
Common data types in Golang include int
, float64
, bool
, string
, and arrays/slices.
package main
import "fmt"
func main() {
age := 20
if age >= 18 {
fmt.Println("You are an adult.")
} else {
fmt.Println("You are a minor.")
}
}
package main
import "fmt"
func main() {
// Classic for loop
for i := 0; i < 5; i++ {
fmt.Println(i)
}
// For - range loop for arrays
numbers := [3]int{1, 2, 3}
for index, value := range numbers {
fmt.Printf("Index: %d, Value: %d\n", index, value)
}
}
package main
import "fmt"
// Function with parameters and return value
func add(a int, b int) int {
return a + b
}
func main() {
result := add(3, 5)
fmt.Println(result)
}
package main
import "fmt"
// Define a struct
type Rectangle struct {
width float64
height float64
}
// Define a method on the Rectangle struct
func (r Rectangle) area() float64 {
return r.width * r.height
}
func main() {
rect := Rectangle{width: 5, height: 10}
fmt.Println(rect.area())
}
package main
import "fmt"
func changeValue(ptr *int) {
*ptr = 100
}
func main() {
num := 20
changeValue(&num)
fmt.Println(num)
}
Pointers in Golang allow you to directly manipulate memory addresses, which is useful for passing data by reference and modifying variables outside the scope of a function.
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)
}
package main
import "fmt"
// Define an interface
type Shape interface {
area() float64
}
// Define a struct that implements the interface
type Circle struct {
radius float64
}
func (c Circle) area() float64 {
return 3.14 * c.radius * c.radius
}
func printArea(s Shape) {
fmt.Println(s.area())
}
func main() {
circle := Circle{radius: 5}
printArea(circle)
}
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 exiting.")
}
package main
import "fmt"
func sendData(ch chan int) {
for i := 0; i < 5; i++ {
ch <- i
}
close(ch)
}
func main() {
ch := make(chan int)
go sendData(ch)
for num := range ch {
fmt.Println(num)
}
}
gofmt
to automatically format your code according to the Golang style guide. For example, run gofmt -w your_file.go
to format a single file.package main
import (
"fmt"
"os"
)
func main() {
file, err := os.Open("nonexistent.txt")
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()
// Use the file...
}
utils
package for utility functions.Transitioning from other languages to Golang may seem daunting at first, but with its simple syntax, powerful features, and strong community support, it becomes an achievable goal. By understanding the fundamental concepts, practicing the basic syntax, and following the common and best practices, you can start writing efficient and scalable Golang applications. Whether you are building web services, system - level tools, or concurrent programs, Golang provides the necessary tools and capabilities.