Skip to content

Files

Latest commit

 

History

History
36 lines (28 loc) · 718 Bytes

how-to-format-string-with-an-integer.md

File metadata and controls

36 lines (28 loc) · 718 Bytes

How to format integer

package main
import "fmt"

func main() {
  res := fmt.Sprintf("Number: %10d", 123)
  fmt.Println(res)
}
  • package main - default package declaration
  • import "fmt" - loads fmt package to operate on strings (and print them)
  • fmt.Sprintf( - formats given string based on a given template and return result
  • %10d - sample number formatting placeholder, will pad number to 10 spaces on the left
  • 123 - sample integer value to format
  • res - variable will contain formatted value

group: int_format

Example:

package main
import "fmt"

func main() {
  res := fmt.Sprintf("Number: %10d", 123)
  fmt.Println(res)
}
Number:        123