In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.Sprint() function in Go language formats using the default formats for its operands and returns the resulting string. Here spaces are added between operands when any string is not used as a constant variable. 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 Sprint(a ...interface{}) string
Here, “a …interface{}” contains some strings including specified constant variables.
Returns: It returns the resulting string.
Example 1:
// Golang program to illustrate the usage of // fmt.Sprint() function // Including the main package package main // Importing fmt, io and os import ( "fmt" "io" "os") // Calling main func main() { // Declaring some const variables const name, dept = "GeeksforGeeks", "CS" // Calling Sprint() function s := fmt.Sprint(name, " is a ", dept, " Portal.\n") // Calling WriteString() function to write the // contents of the string "s" to "os.Stdout" io.WriteString(os.Stdout, s) } |
Output:
GeeksforGeeks is a CS Portal.
Example 2:
// Golang program to illustrate the usage of // fmt.Sprint() function // Including the main package package main // Importing fmt, io and os import ( "fmt" "io" "os") // Calling main func main() { // Declaring some const variables const num1, num2, num3 = 5, 10, 15 // Calling Sprint() function s := fmt.Sprint(num1, num2, num3) // Calling WriteString() function to write the // contents of the string "s" to "os.Stdout" io.WriteString(os.Stdout, s) } |
Output:
5 10 15
In the above code, it can be seen that the function Sprint() is not using any space still space can be seen in the output between the numbers because the function itself added the spaces because not a single string is used as the constant variable.
Example 3:
// Golang program to illustrate the usage of // fmt.Sprint() function // Including the main package package main // Importing fmt, io and os import ( "fmt" "io" "os") // Calling main func main() { // Declaring some const variables const str1, str2, str3 = "a", "b", "c" // Calling Sprint() function s := fmt.Sprint(str1, str2, str3) // Calling WriteString() function to write the // contents of the string "s" to "os.Stdout" io.WriteString(os.Stdout, s) } |
Output:
abc
In the above code, it can be seen that the function Sprint() is not using any space and also spaces can not be seen in the output in between two alphabets this is because strings are used in the constant variables.
Recommended Posts:
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- atomic.AddInt64() Function in Golang With Examples
- atomic.StoreInt64() Function in Golang With Examples
- reflect.FieldByIndex() Function in Golang with Examples
- string.Contains Function in Golang with Examples
- bits.Sub() Function in Golang with Examples
- io.PipeWriter.CloseWithError() Function in Golang with Examples
- time.Round() Function in Golang With Examples
- reflect.AppendSlice() Function in Golang with Examples
- reflect.ChanOf() Function in Golang with Examples
- flag.Bool() Function in Golang With Examples
- time.Sleep() Function in Golang With Examples
- time.Time.Year() Function in Golang with Examples
- reflect.DeepEqual() Function in Golang with Examples
- reflect.Indirect() Function in Golang with Examples
- reflect.CanAddr() Function in Golang with Examples
- reflect.CanInterface() Function in Golang with Examples
- reflect.CanSet() Function in Golang with Examples
- reflect.Cap() Function in Golang with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

