Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 10 of 60 · Go

Working with Files & I/O

Duration: 20 minutes

File I/O

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

The os and io packages provide the building blocks for reading and writing.

// Write a string to a file
f, err := os.Create("out.txt")
if err != nil { log.Fatal(err) }
defer f.Close()
_, err = f.WriteString("Hello, Go!\n")

Buffered I/O

buf := bufio.NewReader(f)
line, err := buf.ReadString('\n')

Working with JSON

type Config struct {
    Port int `json:"port"`
    Debug bool `json:"debug"`
}

var cfg Config
data, _ := os.ReadFile("config.json")
json.Unmarshal(data, &cfg)

Copying files (one‑liner)

io.Copy(dst, src) // src and dst must implement io.Reader / io.Writer

Info

Always check for errors – ignoring them can lead to data loss.

Previous: Pointers, Allocation, and the Garbage CollectorNext: Testing in Go: The `testing` Package