Let's make comprehension easy ...
Go is a high-level general purpose programming language that is statically typed and compiled. It is known for the simplicity of its syntax and the efficiency of development that it enables by the inclusion of a large standard library supplying many needs for common projects.
Build Your First Go Application Step-by-Step
Go (or Golang), developed at Google, is a statically typed, compiled language known for its simplicity, performance, and built-in support for concurrency. It’s a great choice for backend services, cloud tools, and scalable systems.
Visit: https://go.dev/dl/
go version
You should see something like:
go version go1.22.0 darwin/amd64
mkdir hello-go && cd hello-go
go mod init hello-go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
go run main.go
var name string = "Romaan"
age := 30
fmt.Printf("Name: %s, Age: %d\n", name, age)
func add(a int, b int) int {
return a + b
}
if age > 18 {
fmt.Println("Adult")
} else {
fmt.Println("Minor")
}
for i := 1; i <= 5; i++ {
fmt.Println(i)
}
var nums [3]int = [3]int{1, 2, 3}
names := []string{"Alice", "Bob"}
names = append(names, "Charlie")
user := map[string]string{
"name": "Romaan",
"role": "Engineer",
}
fmt.Println(user["name"])
Create a file named main.go
:
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: go run main.go <num1> <num2>")
return
}
a, _ := strconv.Atoi(os.Args[1])
b, _ := strconv.Atoi(os.Args[2])
result := a + b
fmt.Printf("Sum: %d\n", result)
}
go run main.go 4 5
Create mathutils/math.go
:
package mathutils
func Add(a, b int) int {
return a + b
}
Use it in main.go
:
package main
import (
"fmt"
"hello-go/mathutils"
)
func main() {
fmt.Println(mathutils.Add(2, 3))
}
Create mathutils/math_test.go
:
package mathutils
import "testing"
func TestAdd(t *testing.T) {
got := Add(2, 3)
want := 5
if got != want {
t.Errorf("Add(2, 3) = %d; want %d", got, want)
}
}
go test ./...
Comments: