[Python] Golang equivalent to Python Formatted String using F-strings

Discussão em 'Python' iniciado por Stack, Maio 11, 2025.

  1. Stack

    Stack Membro Participativo

    In python one can use f-string to format strings like this

    name1 = 'Ele'
    name2 = 'Ben'
    name3 = 'Frank'
    age = 45

    print(f"My name is {name1} but I also get called {name2} and at times {name3}. Generally I prefer {name1} and my age is {age}")


    what is golang's equivalent to this? where I can specify exactly which variable at what spot

    currently this is all I see with golang, but it creates repeating variables unnecessarily like below

    name1 := "Ele"
    name2 := "Ben"
    name3 := "Frank"
    age := 45

    message := fmt.Sprintf("My name is %s but I also get called %s and at times %s. Generally I prefer %s and my age is %d", name1, name2, name3, name1, age)

    fmt.Println(message)



    Imagine if I need to repeat a variable multiple times in the same string, I will have to keep repeating it and then need to monitor the position of the variable always to make sure they align correctly

    Is there a way similar to f-string in python for golang?

    Continue reading...

Compartilhe esta Página