Go output function

  • date 11th August, 2022 |
  • by Prwatech |
  • 0 Comments

How to Define and Call a Function in Golang

  In Go (or Golang), defining and calling functions is fundamental to structuring and organizing code. Functions in Go are declared using the func keyword followed by the function name, parameters (if any), return types (if any), and the function body enclosed in curly braces. Functions can be defined at package level or within other functions, allowing for modular and reusable code.  
  1. Print()
  2. Println()
  3. Printf()
  1) Print(): function print its arguments with the default format.  
  1.  Program to illustrate print function.
    package main
    
    import "fmt"
    
    func main() {
        var i, j string = "Prwa", "tech"
    
        fmt.Print(i)
        fmt.Print(j)
    
    }
    
    Output :
    PS C:\GO_Language\output> go run output1.go
    Prwa Tech
    
  2. Program to illustrate print function in new line using “\n”.
    package main
    
    import "fmt"
    
    func main() {
        var i, j string = "Data", "Science"
    
        fmt.Print(i, "\n")
        fmt.Print(j, "\n")
    }
    
    Output :
    PS C:\GO_Language\output> go run output2.go
    Data
    Science
    
  3. Program to illustrate print function in a line using “\n”.
    package main
    
    import "fmt"
    
    func main() {
        var i, j string = "Data", "Science"
    
        fmt.Print(i, "\n", j)
    }
    
    Output :
    PS C:\GO_Language\output> go run output3.go
    Python 
    Machine Learning
    
  4. Program to represent the print function of integer values.
    package main
    
    import "fmt"
    
    func main() {
        var i, j = 10, 20
    
        fmt.Print(i, j)
    }
    
    Output :
    PS C:\GO_Language\output> go run output4.go
    10  20
    
     
2) println(): whitespace is added between the arguments, and a newline is added at the end:
package main
import "fmt"

func main() {
    var i, j string = "Parwa", "Tech"
    fmt.Println(i, j)
}
Output :
PS C:\GO_Language\output> go run output5.go    
Parwa Tech
  3) Printf(): first formats its arguments based on the given formatting verb and then print them. %v         use to print the value of the arguments %T        used to print the type of the arguments
package main

import "fmt"

func main() {
    var i string = "PRWATECH"
    var j int = 10

    fmt.Printf("i has value: %v and type: %T\n", i, i)
    fmt.Printf("j has value: %v and type: %T", j, j)
}
Output:
PS C:\GO_Language\output> go run op6.go
  
i has value: PRWATECH and type: string
j has value: 10 and type: int
 
  • Formatting Verb for Printf()
Verb Description  
%v Print the value in the default format
%#v Print the value in Go syntax format
%T Print the type of the value
  5. Program to illustrate printf function of verbs can be use with all data types.
package main
import "fmt"
func main() {
    var i = 10.5
    var X = "PRWATECH"

    fmt.Printf("%v\n", i)
    fmt.Printf("%#v\n", i)
    fmt.Printf("%v%%\n", i)
    fmt.Printf("%T\n", i)

    fmt.Printf("%v\n", X)
    fmt.Printf("%#v\n", X)
    fmt.Printf("%T\n", X)
}
Output :
PS C:\GO_Language\output\p> go run printf1.go
10.5
10.5
10.5%
float64
PRWATECH
"PRWATECH"
string
  6. Program to illustrate printf function of verbs can be use with integer data types.
package main
import "fmt"
func main() {
    var i = 10

    fmt.Printf("%b\n ", i)
    fmt.Printf("%d\n ", i)
    fmt.Printf("%+d\n ", i)
    fmt.Printf("%o\n ", i)
    fmt.Printf("%O\n ", i)
    fmt.Printf("%x\n ", i)
    fmt.Printf("%X\n ", i)
    fmt.Printf("%#x\n ", i)
    fmt.Printf("%4d\n ", i)
    fmt.Printf("%-4d\n ", i)
    fmt.Printf("%04d\n ", i)
}
Output :
PS C:\GO_Language\output\p> go run printf2.go
1010
 10
 +10
 12
 0o12
 a
 A
 0xa
   10
 10  
 0010
How to Define and Call a Function in Golang

Quick Support

image image