How to print new line in golang?
April 26, 2022Hi 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.
- Using the
fmt.Printf()
method - Using the
fmt.Println()
method - Using the
fmt.Sprintf()
method - 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.
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.
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.
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.
All the best 👍