Go Lang Basics Part 1
Part 1 Golang Basics and Fundamentals
Golang, also known as Go, is a free and open-source programming language. It's known for being:
- Simple and easy to learn: Go syntax is clean and straightforward, making it a good choice for beginners.
- Fast and efficient: Go code compiles quickly and produces efficient machine code.
- Concurrent: Go excels at handling multiple tasks simultaneously, making it suitable for building web applications.
Here's a breakdown of some essential Go concepts to get you started:
- Variables and Data Types:
- Variables store data in your program. You declare them with a name and data type (e.g., integer, string).
- Example:
package main import "fmt" func main() { var userName string = "Fajar" // String variable var age int = 30 // Integer variable fmt.Println("Hello,", userName, "you are", age, "years old.") }
- Functions:
- Functions are reusable blocks of code that perform specific tasks. They can take inputs (parameters) and return outputs.
- Example:
package main import "fmt" func greet(name string, age int) { fmt.Println("Hello,", name, "you are", age, "years old.") } func main() { greet("Fajar", 30) }
- Control Flow:
- Control flow statements dictate how your program executes. Common examples include
if/else
statements and loops (for, while). - Example (checking eligibility to vote):
package main import "fmt" func main() { age := 20 if age >= 18 { fmt.Println("You are eligible to vote.") } else { fmt.Println("You are not eligible to vote yet.") } }
- Control flow statements dictate how your program executes. Common examples include
- Packages:
- Packages are reusable blocks of Go code. They help organize your code and improve maintainability.
- Example: You'll typically use packages from the Go standard library (like
fmt
for formatted printing) and potentially external libraries (likefiber
for web development).