当前位置:网站首页>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. The support vector machine (SVM), gaussian kernel function

使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN

uni-app项目总结

Unknown CMake command “add_action_files“

Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation

面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值

Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array

How to design a circular queue?Come and learn~
![[Headline] Written test questions - minimum stack](/img/67/08f2be8afc780e3848371a1b5e04db.png)
[Headline] Written test questions - minimum stack

Ansible中的任务执行控制
随机推荐
鲲鹏编译调试插件实战
A simple file transfer tools
Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
[Headline] Written test questions - minimum stack
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
协作乐高 All In One:DAO工具大全
ROS 动态参数
c语言字符和字符串函数总结(二)
Ansible中的任务执行控制
信息物理系统状态估计与传感器攻击检测
Collection of NFT tools
07-SDRAM :FIFO控制模块
Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
ROS dynamic parameters
工业信息物理系统攻击检测增强模型
短视频seo搜索优化主要内容
已知中序遍历数组和先序遍历数组,返回后序遗历数组
JSP内置对象out对象的功能简介说明
JSP how to obtain the path information in the request object?