Write Files in Go (Challenge)

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This challenge aims to test your ability to write files in Go. You will learn how to write a string or bytes into a file and how to use buffered writers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/FileOperationsGroup(["`File Operations`"]) go/FileOperationsGroup -.-> go/writing_files("`Writing Files`") subgraph Lab Skills go/writing_files -.-> lab-15451{{"`Write Files in Go (Challenge)`"}} end

Writing Files

You need to write a Go program that writes a string and bytes into a file and uses buffered writers.

Requirements

  • The program should write a string and bytes into a file.
  • The program should use buffered writers.

Example

## Try running the file-writing code.
$ go run writing-files.go
wrote 5 bytes
wrote 7 bytes
wrote 9 bytes

## Then check the contents of the written files.
$ cat /tmp/dat1
hello
go
$ cat /tmp/dat2
some
writes
buffered

## Next we'll look at applying some of the file I/O ideas
## we've just seen to the `stdin` and `stdout` streams.

Summary

In this challenge, you learned how to write a string or bytes into a file and how to use buffered writers in Go.

Other Go Tutorials you may like