当前位置:网站首页>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!
边栏推荐
猜你喜欢

Collection of NFT tools

Axure tutorial - the new base (small white strongly recommended!!!)

Short video seo search optimization main content

PHP从txt文件中读取数据的方法

How to design a circular queue?Come and learn~

回顾历史5次经济衰退时期:这一次可能会有何不同?

【HCIP】BGP小型实验(联邦,优化)

Arduino Basic Syntax

微软电脑管家V2.1公测版正式发布

使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
随机推荐
【HCIP】BGP小型实验(联邦,优化)
Day11 shell脚本基础知识
ROS 动态参数
PHP从txt文件中读取数据的方法
Routing strategy
玩转NFT夏季:这份工具宝典值得收藏
QML package management
06-SDRAM : SDRAM control module
双队列实现栈?双栈实现队列?
22.支持向量机—高斯核函数
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
IP Core: FIFO
How does JSP use request to get the real IP of the current visitor?
[头条]笔试题——最小栈
短视频SEO搜索运营获客系统功能介绍
Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
nodeJs--mime模块
JSP内置对象out对象的功能简介说明
go语言标准库fmt包怎么使用
JSP how to obtain the path information in the request object?