MOCKSTACKS
EN
Questions And Answers

More Tutorials








Go Output Functions - Print - Println - Printf

In Go, there are three functions to output text.

Go Print() Function

Go print() function is one of the output functions which print the arguments with their default format

Example

package main
import ("fmt")

func main() {
  var var1,var2 string = "Hello ","Jhon"

  fmt.Print(var1)
  fmt.Print(var2)
}

Output

Hello Jhon


Go Println() Function

Go Println() function is one of the output functions which print a new line every time it's called and it also add a whitespace between printed arguments

Example

package main
import ("fmt")

func main() {
  var var1,var2 string = "Hello","Jhon"

  fmt.Println(var1, var2)
  fmt.Println(var1)
  fmt.Println(var2)
}

Output

Hello Jhon
Hello
Jhon


Go Printf() Function

Go Printf() function is one of the output functions which can print the value and type concatenated to message

  • %v is used to print the value of the arguments
  • %T is used to print the type of the arguments

Example

package main
import ("fmt")

func main() {
  var i string = "Hello"
  var j int = 35
  var k bool = true

  fmt.Printf("i has value: %v and type: %T\n", i, i)
  fmt.Printf("j has value: %v and type: %T", j, j)
  fmt.Printf("k has value: %v and type: %T", k, k)
}

Output

i has value: Hello and type: string
j has value: 35 and type: int
k has value: true and type: bool

Conclusion

In this page (written and validated by ) you learned about Go Output Functions - Print - Println - Printf . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Print Output text in color in console.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.