go fmt code example

Example 1: go format string

s := fmt.Sprintf("string: %s number: %d time: %v", "string", 1, time.Now())

Example 2: print in golang

package Main
//you need to import fmt
import ("fmt")

func main(){
	//then you can use:
	fmt.Print("this message will be printed without a linefeed at the end")
	//or
	fmt.Println("this message will be printed with a linefeed a the end")
	//or										   the \n is a line feed
 	fmt.Printf("You can also use %s formatings with Printf \n (PS : this message won't end with a line feed)","multiples")
  	// will output :
  	/*You can also use multiples formatings with Printf 
      (PS : this message won't end with a line feed)*/
}
// More infos at https://golang.org/pkg/fmt/ or in the source

Example 3: format string go

fmt.Sprintf("foo: %s", bar)

Example 4: go fmt all files

gofmt -w .   # formats files in current directory and all sub-directories
 go fmt ./... # similar to previous
 go fmt .     # formats files in current package
 gofmt -w foo/bar # formats files in directory $PWD/foo/bar and sub-dirs
 go fmt foo/bar   # formats files in directory $GOPATH/src/foo/bar
 gofmt -w     # error, no file or directory specified
 go fmt       # formats files in current package

Example 5: format string go

return fmt.Sprintf("at %v, %s", e.When, e.What)