dict.go 606 B

12345678910111213141516171819202122
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package template
  5. import "fmt"
  6. func dict(values ...interface{}) (map[string]interface{}, error) {
  7. if len(values)%2 != 0 {
  8. return nil, fmt.Errorf("Dict expects an even number of arguments")
  9. }
  10. dict := make(map[string]interface{}, len(values)/2)
  11. for i := 0; i < len(values); i += 2 {
  12. key, ok := values[i].(string)
  13. if !ok {
  14. return nil, fmt.Errorf("Dict keys must be strings")
  15. }
  16. dict[key] = values[i+1]
  17. }
  18. return dict, nil
  19. }