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 chapter07/recipe09.
  2. Navigate to the directory.
  3. Create the rest.go file with the following content:
        package main        import (          "encoding/json"          "fmt"          "io"          "io/ioutil"          "net/http"          "strconv"          "strings"        )        const addr = "localhost:7070"        type City struct {          ID string          Name string `json:"name"`          Location string `json:"location"`        }        func (c City) toJson() string {          return fmt.Sprintf(`{"name":"%s","location":"%s"}`,                             c.Name, c.Location)        }        func main() {          s := createServer(addr)          go s.ListenAndServe()          cities, err := getCities()          if err != nil {            panic(err)          }          fmt.Printf("Retrived cities: %v\n", cities)          city, err := saveCity(City{"", "Paris", "France"})          if err != nil {            panic(err)          } fmt.Printf("Saved city: %v\n", ...

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