PRs are always welcome, though I’m not 100% on splitToMap as a new function. How exactly would it behave? Or did you have another idea for how to simplify this?
Good lord, why did they name them “dict” functions when this is a Go library. I had been searching for map. This is not obvious to gophers… Dictionaries and Dict Functions | sprig
I think I can range and split and use sprig_dict, but it still seems like a poor solution compared to a splitToMap function.
// splitToMap is a version of strings.Split which splits a second time for each
// item in the slice generated from the first split, building a map
func splitToMap(sep1, sep2, s string) (map[string]string, error) {
s = strings.TrimSpace(s)
if s == "" {
emptyMap := make(map[string]string)
return emptyMap, nil
}
s1 := strings.Split(s, sep1)
m:=make(map[string]string, len(s1))
for i := range s1 {
k,v,f:=strings.Cut(s1[i],sep2)
if !f {
continue
}
m[k]=v
}
return m, nil
}