首页 > 其他分享 >golang fmt.Ssanf详解

golang fmt.Ssanf详解

时间:2022-11-03 22:02:43浏览次数:58  
标签:Sscanf string err fmt golang Ssanf main name

Golang fmt.Sscanf()实例讲解
时间:2022-04-07
本文章向大家介绍Golang fmt.Sscanf()实例讲解,主要分析其语法、参数、返回值和注意事项,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
在Go语言中,fmt软件包使用与C的printf()和scanf()函数相似的函数来实现格式化的I /O。 Go语言中的fmt.Sscanf()函数扫描指定的字符串,并将以空格分隔的连续值存储到由格式确定的连续参数中。此外,该函数在fmt包下定义。在这里,您需要导入“fmt”包才能使用这些函数。

用法:

func Sscanf(str string, format string, a ...interface{}) (n int, err error)
参数:此函数接受三个参数,如下所示:

str string:此参数包含将要扫描的指定文本。
format string:对于指定字符串的每个元素,此参数是不同的格式类型。
a …interface{}:此参数接收字符串的每个元素。
返回值:它返回成功解析的项目数。

范例1:

 

// Golang program to illustrate the usage of
// fmt.Sscanf() function

// Including the main package
package main

// Importing fmt
import (
"fmt"
)

// Calling main
func main() {

// Declaring two variables
var name string
var alphabet_count int

// Calling the Sscanf() function which
// returns the number of elements
// successfully parsed and error if
// it persists
n, err:= fmt.Sscanf("GFG is having 3 alphabets.",
"%s is having %d alphabets.", &name, &alphabet_count)

// Below statements get
// executed if there is any error
if err != nil {
panic(err)
}

// Printing the number of
// elements and each elements also
fmt.Printf("%d:%s, %d\n", n, name, alphabet_count)

}
输出:

2:GFG, 3
范例2:

// Golang program to illustrate the usage of
// fmt.Sscanf() function

// Including the main package
package main

// Importing fmt
import (
"fmt"
)

// Calling main
func main() {

// Declaring some variables
var name string
var alphabet_count int
var float_value float32
var boolean_value bool

// Calling the Sscanf() function which
// returns the number of elements
// successfully parsed and error if
// it persists
n, err:= fmt.Sscanf("GeeksforGeeks 13 6.7 true",
"%s %d %g %t", &name, &alphabet_count,
&float_value, &boolean_value)

// Below statements get executed
// if there is any error
if err != nil {
panic(err)
}

// Printing the number of elements
// and each elements also
fmt.Printf("%d:%s, %d, %g, %t", n, name,
alphabet_count, float_value, boolean_value)

}
输出:

4:GeeksforGeeks, 13, 6.7, true

标签:Sscanf,string,err,fmt,golang,Ssanf,main,name
From: https://www.cnblogs.com/cheyunhua/p/16855987.html

相关文章

  • golang封装http get函数请求并且携带header头信息
    有遇到这种需求,golang发送GET请求,携带header头信息,比如header里带着验证token封装函数如下://Get请求携带headerfuncGetWithHeader(urlstring,headersmap[string]st......
  • 【Golang标准库】flag
    参考:flag学习在Golang程序中有很多种方法来处理命令行参数。简单的情况下可以不使用任何库,直接处理os.Args;其实Golang的标准库提供了flag包来处理命令行参数;还有第......
  • golang 重要知识点(二)
    packagemainimport"fmt"typeMyStructstruct{ Namestring}func(sMyStruct)SetName1(namestring){ s.Name=name}func(s*MyStruct)SetName2(name......
  • 协程的简单操作,你都知道哪些?Golang如何实现协程交替打印?
    博主介绍:–我是了凡微信公众号【​​了凡银河系​​】期待你的关注。未来大家一起加油啊~前言对于并发的概念,我们都清楚为了合理利用CPU的执行效率,我们选择当一个事务或......
  • Linux fmt 命令
    Linux命令是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与......
  • 我的Vue之旅 09 数据数据库表的存储与获取实现 Mysql + Golang
    第四期·将部分数据存储至Mysql,使用axios通过golang搭建的http服务器获取数据。新建数据库DROPDATABASEVUE;createdatabaseifnotexistsvue;usevue;JSONTO......
  • golang中的错误处理
    0.1、索引https://waterflow.link/articles/16667167272361、panic当我们执行panic的时候会结束下面的流程:packagemainimport"fmt"funcmain(){ fmt.Println("......
  • Golang获取http Request内容
    获取httpRequest内容获取httpRequest的内容,需要io.ReadAll调用读取了request的Body,读取完后,我们的控制器就没有请求内容了,此时应该需要把读取出来的字节重新构造成一个R......
  • 使用golang操作mongodb
    背景学习使用golang操作mongodb,主要是常见的增删改查。代码packagemainimport("context""fmt""log""go.mongodb.org/mongo-driver/bson"......
  • 优雅处理Golang中的异常
    我们在使用Golang时,不可避免会遇到异常情况的处理,与Java、Python等语言不同的是,Go中并没有try...catch...这样的语句块,我们知道在Java中使用try...catch...这种模式不仅能......