Skip to content

Instantly share code, notes, and snippets.

@alvalea
Created October 4, 2020 18:56
Show Gist options
  • Save alvalea/c59a092ada9e3241a2effb2639f1acc8 to your computer and use it in GitHub Desktop.
Save alvalea/c59a092ada9e3241a2effb2639f1acc8 to your computer and use it in GitHub Desktop.
Read key value configuration into a map
package main
import (
"fmt"
"regexp"
)
var text =
`
"TMLINK" = "ENABLED";
"TIMEOUT" = "20000";
"TTCF" = "1, 2, 3";
`
var pattern = `(?m)^"([^"]+)"\s*=\s*"(([^"]|(\\")|(\\\n))+)";$`
func main() {
config := make(map[string]string)
re := regexp.MustCompile(pattern)
matches := re.FindAllStringSubmatch(text, -1)
for _, m := range matches {
config[m[1]] = m[2]
}
fmt.Printf("%+v\n",config)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment