How to print new line in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

To print a string in a new line in go, you can put \n between the text where you want to start a new line and use the fmt.Printf() method it will show your text in a new line in the console.

See a short example to use \n with the fmt.Printf() method.

fmt.Printf("Hello\nWorld\n")

In this article, you will get four ways to print new lines in golang.

  1. Using the fmt.Printf() method
  2. Using the fmt.Println() method
  3. Using the fmt.Sprintf() method
  4. Using the strings.Join() method

In all the above methods I will use \n to show the string in the next line above methods help you to print text in different use cases.

Let’s start with the fmt.Printf() method.

Using fmt.Printf() method

The fmt package is part of the golang standard library and it provides a lot of options to print statements but here, in this example, you are going to see the example fmt.Printf() with \n.

main.go

package main
import "fmt"

func main() {
    fmt.Printf("Hello\nWorld\n")
}

In the above example, I used \n between Hello World text to print the world in the next line.

OUTPUT

Output:

Hello
World

Using the below link, you will able to edit the above code and customize it as per your requirement.

Try it yourself

Using fmt.Println() method

The fmt package provides one more option to print and that is the fmt.Println() method, in this example, you are going to see an example of fmt.Println() with \n.

main.go

package main
import "fmt"

func main() {
    fmt.Println("Hello\nWorld\n")
}

In the above example, I used \n between Hello World text to print the world in the next line using the Println() method.

OUTPUT

Output:

Hello
World

Using the below link, you will able to edit the above code and customize as per your requirement.

Try it yourself

Using fmt.Sprintf() method

The fmt package provides the Sprintf() method also but it will not help you to print statements into the console, so now you are thinking why it’s here then.

it’s because the Sprintf() method helps you to manipulate your string and return it, here I will not go deep, I will just how to use the fmt.Sprintf() method with \n and print it in the console.

main.go

package main

import "fmt"

func main() {
    message := fmt.Sprintf("Hello\nWorld\n")
    fmt.Println(message)
}

The output will remain the same because at the end we are adding \n in the same text and in the same place.

OUTPUT

Output:

Hello
World

Using the below link, you will able to edit the above code and customize as per your requirement.

Try it yourself

Using strings.Join() method

You can use the strings.Join() method when you have a map ( array ) of string and you want to print each string in a new line.

The strings.Join() method basically help you to concatenate map string with your desired separator but here, I will use \n as a separator.

main.go

package main

import (
    "fmt"
    "strings"
)

func main() {
    message := []string{"Hello", "World"}
    fmt.Println(strings.Join(message, "\n"))
}

The output will remain the same because at the end we are adding \n in the same text and in the same place.

OUTPUT

Output:

Hello
World

Using the below link, you will able to edit the above code and customize it as per your requirement.

Try it yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts