Introduction to Go
## Learning Objectives
- Understand Go history and design philosophy
- Set up Go development environment
- Write and run your first Go program
- Understand Go syntax basics
## What is Go?
Go is a statically typed, compiled programming language developed by Google engineers Robert Griesemer, Rob Pike, and Ken Thompson.
```go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
```
## Why Go?
### Key Features
- **Fast compilation** - Go compiles directly to machine code
- **Simultaneous support** - Concurrency support via goroutines
- **Safety** - Memory safety with garbage collection
- **Clean syntax** - Minimalist and easy to read
- **Static typing** - Catch errors at compile time
- **Standard library** - Rich standard library included
### Design Philosophy
| Principle | Description |
|-----------|-------------|
| Simplicity | Minimal keywords, no complex features |
| Readability | Code should be easy to read and understand |
| Productivity | Fast compile times, easy tooling |
## Installing Go
### Download
Get Go from
### Verify Installation
```bash
go version
```
### Environment
```bash
go env GOPATH
go env GOROOT
```
## Your First Program
### File: main.go
```go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
```
### Run
```bash
go run main.go
```
### Build
```bash
go build -o hello main.go
./hello
```
## Go Program Structure
```go
package main // Package declaration
import "fmt" // Imports
func main() { // Main function (entry point)
// Statements
}
```
### Components
1. **Package**: Group of related source files
2. **Import**: Include other packages
3. **Function**: Reusable code blocks
4. **Main function**: Entry point of executable programs
## Packages
### Main Package
Executable programs must use `package main`.
```go
package main
```
### Other Packages
```go
import "fmt" // Formatted I/O
import "math" // Mathematical functions
import "strings" // String utilities
```
### Multiple Imports
```go
import (
"fmt"
"math"
"strings"
)
```
## fmt Package
### Print
```go
fmt.Println("Hello") // Print with newline
fmt.Print("Hello") // Print without newline
fmt.Printf("Name: %s\n", "Go") // Formatted print
```
### Println vs Print vs Printf
```go
fmt.Println("Hello", "World") // Hello World
fmt.Print("Hello", "World") // HelloWorld
fmt.Printf("%.2f", 3.14159) // 3.14
```
### Format Verbs
| Verb | Description |
|------|-------------|
| `%v` | Default format |
| `%T` | Type of value |
| `%d` | Integer |
| `%f` | Float |
| `%s` | String |
| `%t` | Boolean |
| `%p` | Pointer |
## Comments
```go
// Single-line comment
/*
* Multi-line comment
*/
// TODO: Add more code
// FIXME: Fix this bug
```
## Command Line Arguments
```go
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args
for i, arg := range args {
fmt.Printf("Arg %d: %s\n", i, arg)
}
}
```
```bash
go run main.go arg1 arg2 arg3
```
## Variables
### Declaration
```go
var name string = "Go"
var version = 1.20 // Type inference
name := "Golang" // Short declaration (inside functions)
```
## Basic Data Types
```go
bool // true or false
string // "Hello"
int // Integer (platform-dependent)
int8 // -128 to 127
int32 // -2.1B to 2.1B
float64 // 64-bit float
byte // Alias for uint8
rune // Alias for int32 (Unicode code point)
```
## Naming Conventions
- Use camelCase for variable and function names
- Use PascalCase for exported names
- Keep names short but meaningful
```go
var userName string // camelCase
var UserName string // PascalCase (exported)
const MaxRetries = 3 // PascalCase (constant)
```
## IDEs and Editors
### Popular Editors
- VS Code with Go extension (recommended)
- GoLand (JetBrains)
- Vim/Neovim with vim-go
- Sublime Text with GoSublime
### VS Code Setup
1. Install VS Code
2. Install Go extension
3. Install Go tools: `cmd+shift+p` → "Go: Install/Update Tools"
## Go Versions
```bash
go version # Check installed version
```
| Version | Year | Feature |
|---------|------|---------|
| Go 1.0 | 2012 | Initial release |
| Go 1.5 | 2015 | Self-hosting compiler |
| Go 1.11 | 2018 | Modules |
| Go 1.18 | 2022 | Generics |
| Go 1.21 | 2023 | Improved toolchain |
## Summary
- Go is a statically typed, compiled language designed by Google
- Every executable program needs `package main`
- `go run` executes Go programs directly
- `go build` creates executable binaries
- `fmt` package provides formatted I/O
- Comments use `//` for single-line and `/* */` for multi-line
- Go prioritizes simplicity, readability, and fast compilation
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →