forked from iamshaunjp/golang-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringformatting.go
33 lines (25 loc) · 986 Bytes
/
stringformatting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
import "fmt"
func main() {
age := 35
name := "shaun"
// Print
fmt.Print("hello, ")
fmt.Print("world! \n")
fmt.Print("new line \n")
// Println
fmt.Println("hello ninjas!")
fmt.Println("goodbye ninjas!")
fmt.Println("my age is", age, "and my name is", name)
// Printf (formatted string), %_ = format specifier
fmt.Printf("my age is %v and my name is %v \n", age, name) // %v = value in default format
fmt.Printf("my age is %q and my name is %q \n", age, name) // %q = quotes
fmt.Printf("age is of type %T \n", age) // %T is the type
fmt.Printf("you scored %f points! \n", 225.55) // %f = float format
fmt.Printf("you scored %0.1f points! \n", 225.55) // %0.2f = float with 2 decimal points
// Sprintf (save formatted strings)
var str = fmt.Sprintf("my age is %v and my name is %v \n", age, name)
fmt.Println("the saved string is:", str)
// see more format specifiers here:
// https://golang.org/pkg/fmt/
}