There are a few misconceptions in your code sample. Numbers in a computer aren't formatted, but a string representation of a number can be. Example below.
func main() {
n1 := 12345678 // type = int
fmt.Printf("type: %T, value: %d\n", n1, n1) // outputs "type: int, value: 12345678"
n2 := "12345678" // type = string
fmt.Printf("type: %T, value: %s\n", n2, n2) // outputs "type: string, value: 12345678"
}
See https://golang.org/pkg/fmt/ for where %T
and %d
and %s
come from.
To format a number, I don't think you can get exactly what you want. A few options though:
Below gets you something like what you seem to want based on your question.
func main() {
mynumber := 12345678
mystring := fmt.Sprintf("%d", mynumber)
myformattedstring := fmt.Sprintf("%s-%s %s", mystring[:3], mystring[3:5], mystring[5:])
fmt.Println(myformattedstring)
}
Here is a playground with this code.