当前位置:网站首页>How to use the go language standard library fmt package
How to use the go language standard library fmt package
2022-08-02 00:32:00 【Yisuyun】
How to use the go language standard library fmt package
This article mainly explains "how to use the fmt package of the go language standard library". The content of the explanation in the article is simple and clear, easy to learn and understand. Please follow the ideas of the editor to go deeper and deeper.Come and study and learn "how to use the fmt package of the go language standard library"!
Print series functions
The functions of the same origin as the Print series involve Printf
, Println
and so on, and their meanings are as follows:
Print
: direct output content;
Printf
: formatted output string;
Println
: Add an extra newline at the end of the output.
Next, you can master it directly with the code demonstration.
package mainimport "fmt"func main() { fmt.Print("Output information on a single line") name := "Dream Eraser" fmt.Printf("Blogger: %s\n", name) fmt.Println("Display with newline") fmt.Printf("Blogger: %s\n", name)}
The output is as follows:
Output information on a single line Blogger: Dream Eraser
Display with wrapping
Blogger: Dream Eraser
Where Printf
involves formatting placeholders, see below.
Fprint function
This function starts with F, and it can be basically determined that it is directly related to file operations. In practice, it will be found that this function will output the content to io.Writer
. The test code is as follows:
package mainimport ( "fmt" "os")func main() { // Open the file object and write the exact content fileObj, err := os.OpenFile("./ca.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) if err != nil { fmt.Println("File exception:", err) return } name := "Dream eraser" fmt.Fprintf(fileObj, "Name: %s", name)}
Before writing code, pay attention to importing the os
package first, which is used to manipulate files, and then use os.OpenFile
to create a file object, and then you can perform operations on the file object.Content is written.
Sprint function
The function starts with S
, which must be related to the string. The sample code is as follows:
package mainimport ( "fmt")func main() { // Sprint function s1 := fmt.Sprint("Eraser") name := "Eraser" age := 18s2 := fmt.Sprintf("name:%s,age:%d", name, age) s3 := fmt.Sprintln("Eraser") fmt.Println(s1, s2, s3)}The
Sprint
series of functions will generate a string from the incoming data and return it.
Errorf function
The function formats the string according to the format parameter and returns it.
package mainimport ("fmt")func main() { err := fmt.Errorf("This is an error") fmt.Print(err)}
This content is related to the specific scene and is used directly, which is not much different from ordinary strings.
️ Formatting placeholders
In Go, as long as the function name carries printf
, it supports the format parameter, that is, the placeholder. Let's give a basic description of the placeholder.
General Section
The placeholders all start with %
followed by the concrete type.
%v
: the default form of the value;
%+v
: The output structure will carry the field name;
%#v
: represents the value in Go syntax;
%T
: value type;
%%
: Print the symbol %
.
The following code can be used for testing:
package mainimport ( "fmt")func main() { fmt.Printf("%v\n", 60) fmt.Printf("%v\n", "Eraser") o := struct{ name string }{"Eraser"} fmt.Printf("%v\n", o) fmt.Printf("%#v\n", o) fmt.Printf("%T\n", o) fmt.Printf("60%%\n")}
Integer related
The placeholders associated with integers are:
%b
: binary display;
%c
: unicode encoding display;
%d
: decimal;
%o
: octal;
%x
: hexadecimal lowercase;
%X
: hex uppercase;
%q
: go syntax character literal.
This part is relatively simple, just test it directly, and show it as needed in practice.
Floating point and complex numbers
%b
: scientific notation for binary exponents without fractional part;
%e
: scientific notation, letter e
lowercase;
%E
: same as above, the letter e
is capitalized;
%f
: Display decimals;
%F
: same as above;
%g
: Use %e
or %f
according to the actual situation.
Booleans and pointers
%t
: displayed as true and false;
%p
: Hexadecimal display, and add ox
at the front.
️ stdin
The Go language fmt package contains three standard input functions: fmt.Scan
, fmt.Scanf
, fmt.Scanln
, which are used to obtainUser input information
fmt.Scan
This function gets the text from the standard input. When a newline character is encountered, it means the end of the acquisition. The simple sample code is as follows:
package mainimport ( "fmt")func main() { var ( name string age int ) fmt.Scan(&name, &age) fmt.Printf("Get user inputname:%s age:%d \n", name, age)}
fmt.Scanf
When getting data, you can use the format
parameter, and it is required that the input data must match the corresponding format.
fmt.Scanln
Scanln
is similar to Scan
, it only stops scanning when it encounters a newline.
Fscan and Sscan family of functions
The functions of the above functions are similar to those used in standard output. The functions starting with F are used to manipulate files, and the functions starting with S indicate reading data from the specified string
Thank you for reading, the above is the content of "how to use the fmt package of the go language standard library". After studying this article, I believe that everyone has some questions about how to use the fmt package of the go language standard library.For a deeper experience, the specific usage needs to be verified by everyone.Here is Yisuyun, the editor will push more articles about relevant knowledge points for you, welcome to pay attention!
边栏推荐
猜你喜欢
随机推荐
一文概览最实用的 DeFi 工具
双队列实现栈?双栈实现队列?
146. LRU cache
JSP内置对象out对象的功能简介说明
An interesting project--Folder comparison tool (1)
IO stream basics
微软电脑管家V2.1公测版正式发布
07-SDRAM :FIFO控制模块
els strip deformation
After reshipment tencent greetings to monitor if the corresponding service does not exist by sc. Exe command to add services
632. Minimum interval
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
Arduino Basic Syntax
JSP Taglib指令具有什么功能呢?
链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
玩转NFT夏季:这份工具宝典值得收藏
A simple file transfer tools
短视频SEO优化教程 自媒体SEO优化技巧方法
Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)