go-examples/lower-1line/main.go

47 lines
776 B
Go
Raw Normal View History

2017-07-07 13:27:48 +00:00
package main
import "bufio"
import "io"
import "log"
import "os"
import "strings"
func transform(in io.Reader, out io.Writer) {
bufw := bufio.NewWriter(out)
s := bufio.NewScanner(in)
for s.Scan() {
line := s.Text()
for i, c := range line {
if i == 0 {
bufw.WriteString(strings.ToLower(string(c)))
} else {
bufw.WriteString(string(c))
}
}
bufw.WriteString("\n")
}
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Usage upper-line <file>")
}
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatalln("Error opening file", err)
}
defer f.Close()
var w io.Writer
if len(os.Args) >= 3 {
w, err = os.Create(os.Args[2])
if err != nil {
log.Fatalln("Error creating file", err)
}
} else {
w = os.Stdout
}
transform(f, w)
}