猿问

在 GO 中打印表格的有效方法

好的,所以我想出了一个简单的方法来实现这一目标。


我们首先使用calloc为数组分配内存:


double* c_func(int n_rows) {

    double* result;

    result = calloc(n_rows, sizeof(double));

    for (int i = 0; i < n_rows; ++i) {

        result[i] = (double)i;

    }

    return result;

}

之后,我们只需在 Go 中将数据转换为正确的类型。诀窍是使用C.free释放从 C 端分配的内存。


// convert C double pointer to float64 slice ...

func doubleToFloats(in *C.double, size int) []float64 {

    defer C.free(unsafe.Pointer(in))

    out := (*[1 << 30]float64)(unsafe.Pointer(in))[:size:size]

    return out

}


回首忆惘然
浏览 448回答 2
2回答

MYYA

tabwriter标准库中的包对于这种事情简单易行:w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)fmt.Fprintln(w, "a\tb\tc\td\t")fmt.Fprintln(w, "aa\tbb\tcc\t")fmt.Fprintln(w, "aaa\tbbb\tccc\t")fmt.Fprintln(w, "aaaa\tbbbb\tcccc\tdddd\t")w.Flush()// Prints out:// a&nbsp; &nbsp; b&nbsp; &nbsp; c&nbsp; &nbsp; d&nbsp;// aa&nbsp; &nbsp;bb&nbsp; &nbsp;cc&nbsp; &nbsp;// aaa&nbsp; bbb&nbsp; ccc&nbsp;&nbsp;// aaaa bbbb cccc dddd&nbsp;您可以将 os.Stdout 替换为任何io.Writer.

慕丝7291255

例如,package mainimport (&nbsp; &nbsp; "encoding/csv"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strconv")func main() {&nbsp; &nbsp; outfile := "file.tsv"&nbsp; &nbsp; f, err := os.Create(outfile)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(1)&nbsp; &nbsp; }&nbsp; &nbsp; defer f.Close()&nbsp; &nbsp; w := csv.NewWriter(f)&nbsp; &nbsp; defer w.Flush()&nbsp; &nbsp; w.Comma = '\t'&nbsp; &nbsp; a := []string{"a", "b"}&nbsp; &nbsp; i, j := 0, 1&nbsp; &nbsp; x := float64(2.7)&nbsp; &nbsp; // Write row.&nbsp; &nbsp; err = w.Write(&nbsp; &nbsp; &nbsp; &nbsp; []string{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i], a[j],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strconv.FormatFloat(x, 'f', 4, 64),&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; )&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; }}输出:$ cat file.tsva&nbsp; &nbsp;b&nbsp; &nbsp;2.7000$如果您使用 goroutines,这里有一个使用互斥锁来保护 csv.Writer 的版本。package mainimport (&nbsp; &nbsp; "encoding/csv"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "sync")type csvWriter struct {&nbsp; &nbsp; w *csv.Writer&nbsp; &nbsp; m sync.Mutex}func (c *csvWriter) write(rec []string) error {&nbsp; &nbsp; c.m.Lock()&nbsp; &nbsp; defer c.m.Unlock()&nbsp; &nbsp; return c.w.Write(rec)}func (c *csvWriter) flush() {&nbsp; &nbsp; c.m.Lock()&nbsp; &nbsp; defer c.m.Unlock()&nbsp; &nbsp; c.w.Flush()}func main() {&nbsp; &nbsp; outfile := "file.tsv"&nbsp; &nbsp; f, err := os.Create(outfile)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(1)&nbsp; &nbsp; }&nbsp; &nbsp; defer f.Close()&nbsp; &nbsp; w := csvWriter{w: csv.NewWriter(f)}&nbsp; &nbsp; defer w.flush()&nbsp; &nbsp; w.w.Comma = '\t'&nbsp; &nbsp; a := []string{"a", "b"}&nbsp; &nbsp; i, j := 0, 1&nbsp; &nbsp; x := float64(2.7)&nbsp; &nbsp; // Write row.&nbsp; &nbsp; err = w.write(&nbsp; &nbsp; &nbsp; &nbsp; []string{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i], a[j],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strconv.FormatFloat(x, 'f', 4, 64),&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; )&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Go
我要回答