Standard library / go/printer

go/printer.RawFormat

Bypass tabwriter alignment; overrides UseSpaces. This example checks the resulting source text. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
const RawFormat Mode

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "go/parser"  "go/printer"  "go/token"  "strings")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  fs := token.NewFileSet()  f, e := parser.ParseFile(fs, "demo.go", "package p\nfunc f(){\nvar a=1 // a\nvar longer=2 // b\n_ = a+longer\n}\n", parser.ParseComments)  check(e)  var b bytes.Buffer  cfg := printer.Config{Mode: printer.RawFormat | printer.UseSpaces, Tabwidth: 8}  check(cfg.Fprint(&b, fs, f))  fmt.Println(strings.Contains(b.String(), "\tvar a = 1\t// a"))}
Go Reference