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 chapter01/recipe12.
  2. Navigate to the directory.
  3. Create the main.go file with the following content:
        package main        import (          "encoding/json"          "fmt"          "os"        )        type Client struct {          consulIP string          connString string        }        func (c *Client) String() string {          return fmt.Sprintf("ConsulIP: %s , Connection String: %s",                             c.consulIP, c.connString)        }        var defaultClient = Client{          consulIP: "localhost:9000",          connString: "postgres://localhost:5432",        }        // ConfigFunc works as a type to be used        // in functional options        type ConfigFunc func(opt *Client)        // FromFile func returns the ConfigFunc        // type. So this way it could read the configuration        // from the json.        func FromFile(path string) ConfigFunc { return func(opt ...

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