- Here’s a summary covering the key concepts:
1. Basic Syntax and Data Types
- Variables: Use
var
to declare variables, and you can also use shorthand syntax.var a int = 10 b := 20
- Constants: Use
const
to declare constants.const Pi = 3.14
- Data Types: Basic data types include
int
,float64
,string
, andbool
.
2. Control Structures
- If-Else Statements:
if x > 10 { fmt.Println("x is greater than 10") } else { fmt.Println("x is 10 or less") }
- For Loops: The only looping construct in Go.
for i := 0; i < 10; i++ { fmt.Println(i) }
- Switch Statements:
switch day { case "Monday": fmt.Println("Start of the work week!") case "Friday": fmt.Println("Almost the weekend!") default: fmt.Println("Another day!") }
3. Functions
- Defining Functions:
func add(a int, b int) int { return a + b }
- Multiple Return Values:
func swap(x, y string) (string, string) { return y, x }
4. Structs and Methods
- Structs: Define custom data types.
type Rectangle struct { Width, Height float64 }
- Methods: Functions with a receiver argument.
func (r Rectangle) Area() float64 { return r.Width * r.Height }
5. Arrays, Slices, and Maps
- Arrays: Fixed-size sequences of elements.
var arr [5]int
- Slices: Dynamic arrays.
var slice []int = arr[1:4]
- Maps: Key-value pairs.
var m map[string]int m = make(map[string]int) m["age"] = 25
6. Pointers
- Pointers: Store memory addresses.
var ptr *int ptr = &a
7. Error Handling
- Error Handling: Use the
error
type.func divide(a, b float64) (float64, error) { if b == 0 { return 0, errors.New("division by zero") } return a / b, nil }
8. Concurrency
- Goroutines: Lightweight threads.
go say("world")
- Channels: Communicate between goroutines.
ch := make(chan int) ch <- 42 x := <-ch
9. File Handling
-
Reading and Writing Files:
file, err := os.Open("example.txt") defer file.Close() data, err := ioutil.ReadFile("example.txt")
10. Packages and Imports
- Importing Packages: Use the
import
keyword.import "fmt"
- Creating Packages: Organize your code into packages for better modularity.
11. Standard Library
- fmt: For formatted I/O.
- os: For operating system functionality.
- io/ioutil: For reading and writing files.
- bufio: For buffered I/O.