fmt.Sprintln() Function in Golang With Examples
Last Updated :
05 May, 2020
In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.Sprintln() function in Go language formats using the default formats for its operands and returns the resulting string. Here spaces are always added between operands and a newline is appended at the end. Moreover, this function is defined under the fmt package. Here, you need to import the “fmt” package in order to use these functions.
Syntax:
func Sprintln(a ...interface{}) string
Here, “a …interface{}” is containing some strings along with the specified constant variables.
Returns: It returns the resulting string.
Example 1:
package main
import (
"fmt"
"io"
"os"
)
func main() {
const name, dept = "GeeksforGeeks" , "CS"
s := fmt.Sprintln(name, "is a" , dept, "Portal." )
io.WriteString(os.Stdout, s)
}
|
Output:
GeeksforGeeks is a CS Portal.
Example 2:
package main
import (
"fmt"
"io"
"os"
)
func main() {
const num1, num2, num3, num4 = 5, 10, 15, 50
s1 := fmt.Sprintln(num1, "+" , num2, "=" , num3)
s2 := fmt.Sprintln(num1, "*" , num2, "=" , num4)
io.WriteString(os.Stdout, s1)
io.WriteString(os.Stdout, s2)
}
|
Output:
5 + 10 = 15
5 * 10 = 50
In the above code, no new line or space is used still this function append new line and space between the operands that can be seen in the above output.
Share your thoughts in the comments
Please Login to comment...