Basic Syntax and Data Types in Go 📝
Go features a straightforward syntax resembling C but with modern enhancements. Here are some foundational elements:
- Variables: Declared using
var
or shorthand:=
- Constants: Declared with
const
- Data Types:
int
,float64
,string
,bool
, arrays, slices, maps, structs
Example: Variable Declaration
package main
import "fmt"
func main() {
var age int = 30
name := "Alice"
const Pi float64 = 3.1415
fmt.Println("Name:", name, ", Age:", age, ", Pi:", Pi)
}
Data Types Overview:
| Type | Description |
|--------------------------------|------------------------------------|
| int, int8, int16, int32, int64 | Integer types of varying sizes |
| float32, float64 | Floating point numbers |
| string | Text data |
| bool | Boolean values (true/false) |
| arrays | Fixed-length sequences of elements |
| slices | Dynamic arrays |
| maps | Key-value pairs |
| struct | Composite data types for objects |
Mastering syntax allows you to write clear, efficient, and idiomatic Go code.