go textwriter

2026-08-30 浏览 (1)

textwriter.go 源码

// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// For more tutorials  : https://learngoprogramming.com
// In-person training  : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus

package main

import (
	"fmt"
	"io"
	"sort"
	"strings"
)

// TODO: sort by result key interfaces section

const (

	//          DOMAINS PAGES VISITS UNIQUES
	//             ^      ^     ^    ^
	//             |      |     |    |
	header     = "%-25s %-10s %10s %10s\n"
	line       = "%-25s %-10s %10d %10d\n"
	footer     = "\n%-36s %10d %10d\n" // -> "" VISITS UNIQUES
	dash       = "-"
	dashLength = 58
)

func textWriter(w io.Writer) outputFn {
	return func(res []result) error {
		sort.Slice(res, func(i, j int) bool {
			return res[i].domain > res[j].domain
		})

		var total result

		fmt.Fprintf(w, header, "DOMAINS", "PAGES", "VISITS", "UNIQUES")
		fmt.Fprintf(w, strings.Repeat(dash, dashLength)+"\n")

		for _, r := range res {
			total = total.add(r)
			fmt.Fprintf(w, line, r.domain, r.page, r.visits, r.uniques)
		}

		fmt.Fprintf(w, footer, "", total.visits, total.uniques)

		return nil
	}
}

func noWhere() outputFn {
	return func(res []result) error {
		return nil
	}
}

你可能感兴趣的文章

go textreader

go filters

go result

go chartwriter

go field

go main

go pipeline

go groupers

  • 所属分类: 后端技术
  • 本文标签: golang
  • 版权声明: 本文链接 https://seaxiang.com/blog/3KPdKkTA