url_values_api.mx raw

   1  /*
   2  url.Values is a named map type, and the whole API works through its methods.
   3  
   4  `type Values map[string][][]byte` carries Get, Set, Add, Del, Has and Encode as
   5  methods. A value receiver over a named map is a reference receiver, so Add, Set
   6  and Del write the caller's map. This fixture is the end-to-end guard:
   7  construction, both setters, Get returning the FIRST value, Has, Del, Encode's
   8  sorted output, and ParseQuery feeding the same methods.
   9  */
  10  package main
  11  
  12  import (
  13  	"net/url"
  14  )
  15  
  16  func main() {
  17  	// Termination: a batch program must still be stoppable, so the entry
  18  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  19  	select {
  20  	case <-shutdown:
  21  		return
  22  	default:
  23  	}
  24  
  25  	v := url.Values{}
  26  	v.Set("b", "2")
  27  	v.Add("a", "1")
  28  	v.Add("a", "3")
  29  	if v.Get("a") != "1" {
  30  		panic("get returns the first value")
  31  	}
  32  	if !v.Has("b") {
  33  		panic("has")
  34  	}
  35  	enc := v.Encode()
  36  	if enc != "a=1&a=3&b=2" {
  37  		panic("encode: " | enc)
  38  	}
  39  	parsed, err := url.ParseQuery("x=1&y=2&y=3")
  40  	if err != nil {
  41  		panic("parse")
  42  	}
  43  	if parsed.Get("y") != "2" {
  44  		panic("parsed y")
  45  	}
  46  	parsed.Del("x")
  47  	if parsed.Has("x") {
  48  		panic("del")
  49  	}
  50  	println("values=" | enc)
  51  }
  52