Getting Started With Golang

Golang is a rapidly growing language and dominating other languages

Published on Aug. 30, 2024

Blog Post Image

Getting Started with Go for Web Development

1.1 Why Go for Web Development?

In 2006, Intel introduced its first dual-core processor, leading Google to recognize the potential of multi-core machines. To fully utilize these capabilities, Google began building Go in 2007—a programming language designed to take advantage of multi-core processors.

1.2 Key Benefits of Go

Go was created primarily for server-side development, and Google is migrating its infrastructure to Go.

1.3 Basics of Golang

1.3.1 Packages

Every Go program is made up of packages. The main function is written in the main package. Packages are imported using the import keyword.


package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World")
}
            

The package name should match the last word of the import path.


package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    fmt.Println(utf8.RuneLen('c'))
}
            

1.3.2 Visibility

In Go, names that should be visible in other packages must start with a capital letter.


// Package "notmain" code
package notmain

import (
    "fmt"
    "unicode/utf8"
)

func NewMethod() {
    fmt.Println(utf8.RuneLen('c'))
}

// Package "main" code
package main

import (
    "fmt"
    "notmain"
)

func main() {
    notmain.NewMethod()
}
            

1.3.3 Functions

Function declarations in Go start with the func keyword. Functions can take zero or more arguments, and the return type is specified after the arguments.


package main

import "fmt"

func add(a int, b int) int {
    return a + b
}

func main() {
    fmt.Println("12 + 12 :", add(12, 12))
}
            

1.3.4 Variables

The var keyword is used to declare variables. Variables can be at the function level or package level.


package main

import "fmt"

var age int
var name string

func main() {
    age = 21
    name = "Name"
    fmt.Printf("Name: %s\nAge: %d", name, age)
}
            

Variables can also be declared using the := operator, which automatically assigns the data type and value to the variable. This method can only be used within a function.


func myfunc() {
    age := 21
    fmt.Printf("age: %v\ndata type: %T", age, age)
}
            

Conclusion

Golang is a rapidly growing language and dominating other languages