|
|
|
|
|
by dewey
2954 days ago
|
|
As this seems to be a beginner project to learn here's some thoughts on the code: 1) You should really not build your json by manually building the string. That's very error prone and hard to extend. fmt.Fprintf(f,
"{\n\t\"from\": {\n\t\t\"name\": \"%s\",
Use https://golang.org/pkg/encoding/json/ (https://gobyexample.com/json) instead. Working with json in Go is really one of the nicer features of Go (It's Go, not "GoLang")2) By setting / typoing a config option you could accidentally delete files from your computer, even your home directory err := os.Remove(jsonfile)
|
|
To assist in using JSON, you'll want to declare a struct holding all your config, at which point it becomes quite surprisingly simple to load it from and save it to a file.
Once you have that functionality, I suggest using it for your initial creation as well. I have a program here where I've been using the pattern that I create a default config sample within the program, and if I can't find the config you specify, I print out an error, and the sample JSON file that constitutes a full, legal config for the user's convenience. I wouldn't ship anything that I'd expect to end up in a Linux distro or something that way, but for an internal tool it seems a decent enough pattern.
Finally, I'd suggest reddit.com/r/golang is a better sort of place for this sort of thing if you want a review, and there's probably even better places, as I'm not convinced that a karma-based voting site is all that great for reviews for beginners. (It's really easy for a post like that to pick up a couple of early downvotes, and consequently lose all visibility to the people who are willing to help.) But if you're going to use one, /r/golang would be better.