go-examples/lower-1line/main.go
2017-07-07 15:27:48 +02:00

47 lines
776 B
Go

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)
}