Created
October 4, 2020 18:56
-
-
Save alvalea/c59a092ada9e3241a2effb2639f1acc8 to your computer and use it in GitHub Desktop.
Read key value configuration into a map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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