Add formatting

This commit is contained in:
Sangeeth Sudheer 2024-05-01 20:12:00 +05:30
parent ab676f25a6
commit 18da65a933
Signed by: x
GPG Key ID: F6D06ECE734C57D1
2 changed files with 40 additions and 2 deletions

36
formatting/formatting.go Normal file
View File

@ -0,0 +1,36 @@
package formatting
import (
"fmt"
"math"
)
var f = fmt.Printf
type Person struct {
name string
age uint
}
func Formatting() {
p := Person{"Nadeshiko", 15}
f("int: %d\n", 5)
f("float: %f\n", 4)
f("float (scientific): %e\n", math.Pow(2, 32))
f("string: %s\n", "hello")
f("go value loose: %v\n", p)
f("go value strict: %+v\n", p)
f("go value verbose: %#v\n", p)
f("go bool: %t\n", true)
f("go type: %T\n", p)
f("go ptr address: %p\n", &p)
f("go hex string: %x\n", "foo")
f("go hex int: %x\n", 16)
f("go bin int: %b\n", 5)
f("go oct int: %o\n", 8)
f("go num width: |%5d|\n", 2)
f("go num width (left align): |%-5d|\n", 2)
f("go float precision (left align): |%-5.2f|\n", 3.141516)
f("go char: %c\n", 97)
}

View File

@ -22,7 +22,8 @@ package main
// import "git.sangeeth.dev/gobyexample/panic" // import "git.sangeeth.dev/gobyexample/panic"
// import "git.sangeeth.dev/gobyexample/defers" // import "git.sangeeth.dev/gobyexample/defers"
// import "git.sangeeth.dev/gobyexample/recovering" // import "git.sangeeth.dev/gobyexample/recovering"
import "git.sangeeth.dev/gobyexample/stringfuncs" // import "git.sangeeth.dev/gobyexample/stringfuncs"
import "git.sangeeth.dev/gobyexample/formatting"
func main() { func main() {
// runes.Runes() // runes.Runes()
@ -47,5 +48,6 @@ func main() {
// panic.Panic() // panic.Panic()
// defers.Defers() // defers.Defers()
// recovering.Recover() // recovering.Recover()
stringfuncs.StringFuncs() // stringfuncs.StringFuncs()
formatting.Formatting()
} }