当前位置:网站首页>Go notes (3) usage of go language FMT package

Go notes (3) usage of go language FMT package

2022-07-04 20:47:00 fiveym

Introduce

fmt Is a common library for input and output
stay fmt package , There are two kinds of methods for formatting input and output :scan and print
Respectively in scan.go and print.go In file

print: Output function

print Series is mainly used for output , It mainly includes three methods :

print: Output content directly ,  Don't wrap  , Cannot format output .
printf:  Format the text output as specified .
println: Can be behind the output   Add line breaks  .
package main 

import "fmt" 
func main() {
     
//  When entering multiple values at once  Println  There's a space in the middle , Can wrap lines automatically  
    fmt.Println("Hello", "Println") 
//  When entering multiple values at once  Print  No middle space ,Print  No line wrapping ; 
    fmt.Print("Hello", "Print") 
// Printf  Is to format the output , In many scenarios  Println  More convenient ,Printf I won't change lines  
    fmt.Printf("Hello Printf %s", "AAA") 
}
// Hello Println 
// HelloPrintHello Printf AA

print.go It's defined in the file 9 A function
this 9 A function , According to two dimensions, functions can be divided according to two dimensions

   // If you put  Print  Understood as core keywords , Then the suffix followed by " f " and " ln " as well as "", Focus on the final result of the output content ; If the suffix is " f ",  Specify the format  If the suffix is " ln ",  There is a newline character .
   Println、Fprintln、Sprintln  Line breaks will be added when outputting content ; 
   Print、Fprint、Sprint  Output content without line breaks ; 
   Printf、Fprintf、Sprintf  Format the text output as specified .
   // If you put  Print  Understood as core keywords , Then the prefix in front has " F " and " S " as well as "", Focus on the goal of output content ( terminal ); If the prefix is " F ",  Specify the  io.Writer  If the prefix is " S ",  Is output to string 
   Print、Printf、Println  Output content to standard output os.Stdout; 
   Fprint、Fprintf、Fprintln  Output content to the specified io.Writer;    
   Sprint、Sprintf、Sprintln  Output content to string 

scan: Output function

scan Series is mainly used to input
Example: get user input in the interactive interface

package main 

import "fmt" 

func main() {
     
     var name string 
     fmt.Print(" Enter your name :") 
     fmt.Scan(&name) 
     fmt.Printf(" The name you entered is :%s", name) 
}
// Enter your name : Wang Jingze  
// The name you entered is : Wang Jingze 
// It is worth noting that ,Scan  You need to use  &, Otherwise, it will be regarded as direct transmission .

scan.go It's defined in the file 9 A function :
this 9 Functions can scan formatted text to generate values . It can also be explained according to two dimensions .

/* If you put " Scan " Understood as core keywords , Then the suffix followed by " f " and " ln " as well as "",  Focus on the results of the input ; If the suffix is " f ",  Specify the format  If the suffix is " ln ",  There is a newline character */

Scanln、Fscanln、Sscanln  Stop when reading line breaks , And ask for one line of all entries at a time ; 
Scan、Fscan、Sscan  Don't pay attention to line breaks when reading content ; 
Scanf、Fscanf、Sscanf  Read from formatted text .

/* If you put " Scan " Understood as core keywords , Then the prefix in front has " F " and " S " as well as "",  Focus on the source of input content ( terminal ); If the prefix is " F ",  Specify the  io.Reader  If the prefix is " S ",  Is read from the string */

Scan、Scanf、Scanln  From standard input os.Stdin Read text ; 
Fscan、Fscanf、Fscanln  From the specified io.Reader Interface reads text ; 
Sscan、Sscanf、Sscanln  Read text from a parameter string .
原网站

版权声明
本文为[fiveym]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041902540924.html