O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter05/recipe05.
  2. Navigate to the directory.
  3. Create the charset.go file with the following content:
        package main        import (          "fmt"          "io/ioutil"          "os"          "golang.org/x/text/encoding/charmap"        )        func main() {          // Write the string          // encoded to Windows-1252          encoder := charmap.Windows1252.NewEncoder()          s, e := encoder.String("This is sample text with runes Š")          if e != nil {            panic(e)          }          ioutil.WriteFile("example.txt", []byte(s), os.ModePerm)          // Decode to UTF-8          f, e := os.Open("example.txt")          if e != nil {            panic(e)          }          defer f.Close()          decoder := charmap.Windows1252.NewDecoder()          reader := decoder.Reader(f)          b, err := ioutil.ReadAll(reader)          if err != nil {            panic(err)          }          fmt.Println(string(b))        }
  1. Execute ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required