add Printfln to log and fmt

74 views
Skip to first unread message

rick...@gmail.com

unread,
May 22, 2018, 7:29:59 PM5/22/18
to golang-nuts
I have a rather simple request - I am not sure if this already exists but I'd like to avoid typing "\n" everytime I do Printf( ) even though I quite enjoy doing that. Is there an inbuilt library function that does this?

Thanks
Anirudh

Karan Chaudhary

unread,
May 23, 2018, 3:35:36 AM5/23/18
to golang-nuts
I don't know if its a good idea to have this in stdlib,  but you can definitely write your own to solve the problem :)

package main

import (
"fmt"
)

func Printfln(format string, a ...interface{}) {
fmt.Printf(format + "\n", a...)
}

func main() {
Printfln("%s is awesome!", "Go")
fmt.Println("yup")
}

Jan Mercl

unread,
May 23, 2018, 3:49:28 AM5/23/18
to rick...@gmail.com, golang-nuts
On Wed, May 23, 2018 at 1:29 AM <rick...@gmail.com> wrote:

> Is there an inbuilt library function that does this?

There's not. However note that the log package adds the final \n automatically to every output not already having it.

--

-j

Reply all
Reply to author
Forward
0 new messages