Video Coming Soon...
41: cut
This exercise is pending. Quick notes about this exercise:
- My implemention is at https://lcthw.dev/go/go-coreutils
- https://www.gnu.org/software/coreutils/manual/html_node/cut-invocation.html
- bufio, io -- https://pkg.go.dev/bufio@go1.25.3
- maps -- https://pkg.go.dev/maps@go1.25.3
- slices -- https://pkg.go.dev/slices@go1.25.3
- strings -- https://pkg.go.dev/strings@go1.25.3
The Code
View Source file go-coreutils/cut/main.go Only
package main
import (
"fmt"
"flag"
"strings"
"log"
"strconv"
"bufio"
"os"
)
type Opts struct {
FieldOpt string
Fields []int
Delim string
}
func parse_opts() Opts {
var opts Opts
opts.Fields = make([]int, 0, 10)
flag.StringVar(&opts.FieldOpt, "f", "", "fields list")
flag.StringVar(&opts.Delim, "d", "\t", "delimiter byte")
flag.Parse()
if opts.FieldOpt == "" { log.Fatal("-f fields list required") }
for _, num := range strings.Split(opts.FieldOpt, ",") {
as_int, err := strconv.Atoi(num)
if err != nil { log.Fatal("-f can only contain numbers") }
opts.Fields = append(opts.Fields, as_int)
}
return opts
}
func main() {
opts := parse_opts()
scan := bufio.NewScanner(os.Stdin)
for scan.Scan() {
line := scan.Text()
chopped := strings.Split(line, opts.Delim)
for i, field_num := range opts.Fields {
if field_num >= len(chopped) {
fmt.Print("\n")
continue
}
fmt.Print(chopped[field_num])
if i < len(opts.Fields) - 1 {
fmt.Print(opts.Delim)
} else {
fmt.Print("\n")
}
}
}
}
Register for Learn Go the Hard Way
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.