So I really want to learn go, however its a much harder and confusing language than I thought it would be. My goal is to write a program that edits a dns file. The first step is to open a file and read it into memory. Ok, that was easily accomplished with ioutil.Readfile. So the next thing I need to do is to read only the SOA or read until you encounter a ")". How on earth would I do this? Here is what the file looks like:
example.com. SOA dns1.example.com. hostmaster.example.com. (
2001062501 ; serial
21600 ; refresh after 6 hours
3600 ; retry after 1 hour
604800 ; expire after 1 week
86400 ) ; minimum TTL of 1 day
Ideally the program would start with the line that included "SOA" and end with ")" and also recognize ";". Where should I start with this?
One way to do have a total control is to use text/scanner
Here is an example of your case: http://play.golang.org/p/_9LddtB7R9
package main
import (
"fmt"
"strings"
)
import "text/scanner"
var dnsRecord = `example.com. SOA dns1.example.com. hostmaster.example.com. (
2001062501 ; serial
21600 ; refresh after 6 hours
3600 ; retry after 1 hour
604800 ; expire after 1 week
86400 ) ; minimum TTL of 1 day
`
func main() {
var s scanner.Scanner
s.Init(strings.NewReader(dnsRecord))
tok := s.Scan()
for tok != scanner.EOF {
// do something with tok
tok = s.Scan()
fmt.Println(s.TokenText())
}
}
Otherwise you could use a Regex to match a specific line http://golang.org/pkg/regexp/