How to format the printf function to specify a width for variables with different datatypes in Go

1 Answer

0 votes
package main

import (
    "fmt"
)

func main() {
    fmt.Printf("a: |%6d|%6d|\n", 45, 980)

    fmt.Printf("b: |%6.2f|%6.2f|\n", 4.8, 3.14) // right-justify

    fmt.Printf("c: |%-6.2f|%-6.2f|\n", 4.8, 3.14) // left-justify

    fmt.Printf("d: |%6s|%6s|\n", "Go", "Y")

    fmt.Printf("e: |%-6s|%-6s|\n", "Go", "Y")
}



/*
run:

a: |    45|   980|
b: |  4.80|  3.14|
c: |4.80  |3.14  |
d: |    Go|     Y|
e: |Go    |Y     |

*/

 



answered Dec 1, 2024 by avibootz

Related questions

...