0% found this document useful (0 votes)
7 views1 page

Algo Practice in Go

This document discusses programmatically formatting Go code using the go/format package. It shows an unformatted code sample being passed to the format.Source function which returns the formatted code or any errors.

Uploaded by

kai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Algo Practice in Go

This document discusses programmatically formatting Go code using the go/format package. It shows an unformatted code sample being passed to the format.Source function which returns the formatted code or any errors.

Uploaded by

kai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Code can be formatted programmatically in the same way like running go fmt,

// using the go/format package


package main

import (
"fmt"
"go/format"
"log"
)

func Example() {
unformatted := `
package main
import "fmt"

func main( ) {
x := 12
fmt.Printf( "%d", x )
}

`
formatted, err := format.Source([]byte(unformatted))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", string(formatted))
// Output:
// package main
//
// import "fmt"
//
// func main() {
// x := 12
// fmt.Printf("%d", x)
// }
}

You might also like