当前位置:网站首页>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!
边栏推荐
猜你喜欢
22.支持向量机—高斯核函数
短视频SEO搜索运营获客系统功能介绍
单片机遥控开关系统设计(结构原理、电路、程序)
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
解析正则表达式的底层实现原理
REST会消失吗?事件驱动架构如何搭建?
Pytorch seq2seq 模型架构实现英译法任务
认识USB、Type-C、闪电、雷电接口
面试必问的HashCode技术内幕
随机推荐
Collection of NFT tools
632. 最小区间
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
如何优雅的消除系统重复代码
When Netflix's NFTs Forget Web2 Business Security
不就是个TCC分布式事务,有那么难吗?
[21-Day Learning Challenge] A small summary of sequential search and binary search
Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
Cyber-Physical System State Estimation and Sensor Attack Detection
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
控制电机的几种控制电路原理图
IO stream basics
JSP out. The write () method has what function?
22.支持向量机—高斯核函数
bgp 聚合 反射器 联邦实验
基于注意力机制的多特征融合人脸活体检测
Statement执行update语句
An overview of the most useful DeFi tools
Routing strategy
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散