100daysofcode - Day 17
Hello folks, a new day is here, let’s wrap it successfully by learning some new techniques to format a string in Golang. ![]()
![]()
![]()
In yesterday post we talked about the string functions and how we can use them to manipulate strings in Go ![]()
![]()
In today’s post we will learn the different ways to format a string, by moving from the boring theory part, to directly moving on directly to an interactive example to learn from it. ![]()
![]()
![]()
Exercise
package main
import (
"fmt"
)
type point struct {
x, y int
}
func main() {
p := point{5, 7}
fmt.Printf("struct: %v\n", p)
// The %v print the struct values as they are in the
// Output: struct: {5 7}
fmt.Printf("struct: %+v\n", p)
// The %+v print the struct values with the struct field names
// Output: struct: {x:5 y:7}
fmt.Printf("struct: %#v\n", p)
// The %#v print the struct values, field names and the syntax representation
// Output: struct: main.point{x:5, y:7}
fmt.Printf("type: %T\n", p)
// The %T print the type of the variable
// Output: type: main.point
fmt.Printf("int: %d\n", 123)
// The %d print the value in base 10 representation
// Output: 123
fmt.Printf("bin: %b\n", 14)
// The %b print the value in binary
// Output: 1110
fmt.Printf("char: %c\n", 33)
// The %c print the char corresponding to the integer
// char: !
fmt.Printf("hex: %x\n", 456)
// The %x print the value in hexadecimal
// Output: 1c8
fmt.Printf("float1: %f\n", 78.9)
// The %f print the float number in it's basic form
// Output: 78.900000
fmt.Printf("float2: %e\n", 123400000.0)
fmt.Printf("float3: %E\n", 123400000.0)
// The %e and %E print the float value in different way
// Output: 1.234000e+08 or 1.234000E+08
fmt.Printf("str: %s\n", "\"string\"")
// The %s is used to print the string
// Output: "string"
fmt.Printf("str: %q\n", "\"string\"")
// The %q is used to double quote strings
// Output: "\"string\""
fmt.Printf("str: %x\n", "hex this")
// The %x render the string in base 16
// Output: 6865782074686973
fmt.Printf("pointer: %p\n", &p)
// The %p is used to print the representation of the pointer
// Output: 0x1400012a010
}
Let’s end
this amazing post now,
I hope you can learn some amazing stuff from it, and stay tuned for tomorrow’s post where we will explore new interesting topics. ![]()
![]()