How to do it...

The following steps cover how to write and run your application:

  1. From your Terminal/console application, create a new directory called ~/projects/go-programming-cookbook/chapter3/encoding.
  2. Navigate to this directory.
  1. Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter3/encoding

You should see a file called go.mod that contains the following:

module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter3/encoding    
  1. Copy tests from ~/projects/go-programming-cookbook-original/chapter3/encoding or use this as an exercise to write some of your own code!
  2. Create a file called gob.go with the following content:
        package encoding

import (
"bytes"
"encoding/gob"
"fmt"
)

// pos stores the x, y position
// for Object
type pos struct {
X int
Y int
Object string
}

// GobExample demonstrates using
// the gob package
func GobExample() error {
buffer := bytes.Buffer{}

p := pos{
X: 10,
Y: 15,
Object: "wrench",
}

// note that if p was an interface
// we'd have to call gob.Register first

e := gob.NewEncoder(&buffer)
if err := e.Encode(&p); err != nil {
return err
}

// note this is a binary format so it wont print well
fmt.Println("Gob Encoded valued length: ",
len(buffer.Bytes()))

p2 := pos{}
d := gob.NewDecoder(&buffer)
if err := d.Decode(&p2); err != nil {
return err
}

fmt.Println("Gob Decode value: ", p2)

return nil
}
  1. Create a file called base64.go with the following content:
        package encoding

import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
)

// Base64Example demonstrates using
// the base64 package
func Base64Example() error {
// base64 is useful for cases where
// you can't support binary formats
// it operates on bytes/strings

// using helper functions and URL encoding
value := base64.URLEncoding.EncodeToString([]byte("encoding
some data!"))
fmt.Println("With EncodeToString and URLEncoding: ", value)

// decode the first value
decoded, err := base64.URLEncoding.DecodeString(value)
if err != nil {
return err
}
fmt.Println("With DecodeToString and URLEncoding: ",
string(decoded))

return nil
}

// Base64ExampleEncoder shows similar examples
// with encoders/decoders
func Base64ExampleEncoder() error {
// using encoder/ decoder
buffer := bytes.Buffer{}

// encode into the buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buffer)

if _, err := encoder.Write([]byte("encoding some other
data")); err != nil {
return err
}

// be sure to close
if err := encoder.Close(); err != nil {
return err
}

fmt.Println("Using encoder and StdEncoding: ",
buffer.String())

decoder := base64.NewDecoder(base64.StdEncoding, &buffer)
results, err := ioutil.ReadAll(decoder)
if err != nil {
return err
}

fmt.Println("Using decoder and StdEncoding: ",
string(results))

return nil
}
  1. Create a new directory named example and navigate to it.
  2. Create a file called main.go with the following content:
        package main

import (
"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter3/encoding"
)

func main() {
if err := encoding.Base64Example(); err != nil {
panic(err)
}

if err := encoding.Base64ExampleEncoder(); err != nil {
panic(err)
}

if err := encoding.GobExample(); err != nil {
panic(err)
}
}
  1. Run go run main.go. You could also run the following:
$ go build
$ ./example

You should see the following output:

$ go run main.go
With EncodeToString and URLEncoding:
ZW5jb2Rpbmcgc29tZSBkYXRhIQ==

With DecodeToString and URLEncoding: encoding some data!
Using encoder and StdEncoding: ZW5jb2Rpbmcgc29tZSBvdGhlciBkYXRh
Using decoder and StdEncoding: encoding some other data
Gob Encoded valued length: 57
Gob Decode value: {10 15 wrench}
  1. If you copied or wrote your own tests, go up one directory and run go test. Ensure that all tests pass.