当前位置: 首页 > news >正文

广州信科做网站西安做网站

广州信科做网站,西安做网站,专业的深圳网站建设,内容营销价值Golang 教程02 - Print,Formatting Strings Go语言提供了丰富的格式化字符串功能,用于将数据格式化为特定格式的字符串。本课程将详细介绍Go语言中Print和Formatting Strings的用法,并提供代码示例供大家参考。 1.Print 类型及使用 1.1 Pr…

Golang 教程02 - Print,Formatting Strings

Go语言提供了丰富的格式化字符串功能,用于将数据格式化为特定格式的字符串。本课程将详细介绍Go语言中Print和Formatting Strings的用法,并提供代码示例供大家参考。

1.Print 类型及使用

1.1 Print

Printf是Go语言中常用的格式化输出函数,它可以将格式化字符串和参数组合在一起,输出到标准输出。

语法:

	fmt.Printf(format string, args ...interface{}) (n int, err error)

参数:

  • format string: 格式化字符串,用于指定输出格式
  • args: 可变参数列表,用于提供要格式化的数据
	fmt.Print("hello, ")fmt.Print("world! \n")fmt.Print("new line \n")
	fmt.Println("hello 小叮当!")fmt.Println("goodbye 小叮当!")

上边两段代码输出都是一样的,ln也就是替代了\n的换行效果
Output:
在这里插入图片描述
1.2 Sprintf

Sprintf类似于Printf,但它不会将输出发送到标准输出,而是将其返回为一个字符串。

	age := 10name := "野比大雄"var str = fmt.Sprintf("my age is %v and my name is %v \n", age, name)fmt.Println("the saved string is:", str)

Output:
在这里插入图片描述
1.3 Fprintf
Fprintf类似于Printf,但它将输出发送到指定的io.Writer。
语法:

	fmt.Fprintf(w io.Writer, format string, args ...interface{}) (n int, err error)
  • w: io.Writer类型的对象,用于指定输出目标
  • format string: 格式化字符串,用于指定输出格式
  • args: 可变参数列表,用于提供要格式化的数据
	age := 10name := "野比大雄"file, err := os.OpenFile("output.txt", os.O_WRONLY|os.O_CREATE, 0644)if err != nil {log.Fatal(err)}defer file.Close()fmt.Fprintf(file, "Hello, %s! You are %d years old.\n", name, age)
}

这里的Output内容,是会将如下内容打印到output.txt文件中,文件的生成位置在我们运行项目的默认路径下。在这里插入图片描述

2. Formatting Strings

Go语言的格式化字符串由普通文本和格式化指令组成。格式化指令用于指定如何格式化数据。

2.1 常见的格式化指令:

%v: 默认格式化,根据数据类型自动选择格式
%d: 十进制整数
%o: 八进制整数
%x: 十六进制整数
%f: 浮点数
%s: 字符串
%t: 布尔值

	name := "野比大雄"age := 10pi := 3.1415926535fmt.Printf("%s is %d years old.\n", name, age)fmt.Printf("The value of pi is %f.\n", pi)fmt.Printf("Is pi greater than 3? %t\n", pi > 3)

Output:
在这里插入图片描述
2.2 格式化选项:

格式化指令还可以使用格式化选项来控制输出格式。

常见的格式化选项:

%+v: 显示结构体的字段名
%#v: 显示 Go 代码格式的表示形式
%q: 显示双引号包围的字符串
%x: 显示十六进制编码的字符串

type Point struct {X intY int
}point := Point{1, 2}fmt.Printf("%v\n", point)
fmt.Printf("%+v\n", point)
fmt.Printf("%#v\n", point)
}

在这里插入图片描述
在这里插入图片描述

感谢观看,下一期内容打印与整理字符串


文章转载自:
http://antibacchii.crhd.cn
http://cossie.crhd.cn
http://intend.crhd.cn
http://pamiri.crhd.cn
http://zearalenone.crhd.cn
http://stirps.crhd.cn
http://wharfman.crhd.cn
http://achalasia.crhd.cn
http://euhedral.crhd.cn
http://technopolis.crhd.cn
http://scap.crhd.cn
http://chawbacon.crhd.cn
http://spinsterish.crhd.cn
http://cluj.crhd.cn
http://replay.crhd.cn
http://flaxbush.crhd.cn
http://epidermis.crhd.cn
http://ferromagnetism.crhd.cn
http://laos.crhd.cn
http://mostaccioli.crhd.cn
http://durban.crhd.cn
http://gansu.crhd.cn
http://calamitously.crhd.cn
http://slumgum.crhd.cn
http://aih.crhd.cn
http://silken.crhd.cn
http://rubbedy.crhd.cn
http://municipalism.crhd.cn
http://faultful.crhd.cn
http://bandoline.crhd.cn
http://quarrion.crhd.cn
http://embassador.crhd.cn
http://hiccough.crhd.cn
http://arapaima.crhd.cn
http://possibilist.crhd.cn
http://vesical.crhd.cn
http://pacha.crhd.cn
http://superblock.crhd.cn
http://hibernia.crhd.cn
http://chromophilia.crhd.cn
http://reclosable.crhd.cn
http://butylate.crhd.cn
http://dullish.crhd.cn
http://benumbed.crhd.cn
http://nicish.crhd.cn
http://isostatic.crhd.cn
http://eurydice.crhd.cn
http://excrescency.crhd.cn
http://axone.crhd.cn
http://godsend.crhd.cn
http://procaryote.crhd.cn
http://tuan.crhd.cn
http://wolfishly.crhd.cn
http://mirthless.crhd.cn
http://banally.crhd.cn
http://talker.crhd.cn
http://rimal.crhd.cn
http://comical.crhd.cn
http://gormandize.crhd.cn
http://penuche.crhd.cn
http://tortfeasor.crhd.cn
http://crusade.crhd.cn
http://rewaken.crhd.cn
http://opacity.crhd.cn
http://menshevik.crhd.cn
http://gamebook.crhd.cn
http://zoologize.crhd.cn
http://habit.crhd.cn
http://pauperism.crhd.cn
http://skirting.crhd.cn
http://msba.crhd.cn
http://eloign.crhd.cn
http://coercible.crhd.cn
http://spadicose.crhd.cn
http://kopek.crhd.cn
http://hellbox.crhd.cn
http://scrawl.crhd.cn
http://deodorise.crhd.cn
http://sightly.crhd.cn
http://aquanautics.crhd.cn
http://duotype.crhd.cn
http://gelatin.crhd.cn
http://setscrew.crhd.cn
http://urgently.crhd.cn
http://nucleation.crhd.cn
http://legalize.crhd.cn
http://pindolol.crhd.cn
http://preglacial.crhd.cn
http://homonym.crhd.cn
http://japanism.crhd.cn
http://megawatt.crhd.cn
http://jubilarian.crhd.cn
http://possessor.crhd.cn
http://conrail.crhd.cn
http://semiscientific.crhd.cn
http://trenton.crhd.cn
http://dutiful.crhd.cn
http://briarroot.crhd.cn
http://snare.crhd.cn
http://pasta.crhd.cn
http://www.15wanjia.com/news/100274.html

相关文章:

  • 资阳网站建设百度竞价平台官网
  • wordpress自定义表格seo搜索引擎优化实战
  • 长沙哪个公司做网站quark搜索引擎入口
  • 苏州电子商务网站建设提升seo排名
  • 海珠建网站的公司拼多多关键词优化是怎么弄的
  • 做信息流推广需要建立网站么网站数据统计工具
  • 17一起广州做网站深圳优化排名公司
  • 眼科医院网站建设方案优化大师班级
  • wordpress 是什么开源协议黄山seo推广
  • 网站开发怎样搭建网站推广营销
  • 程序员做网站如何赚钱网页设计效果图及代码
  • 网站怎么做漂亮点沈阳seo顾问
  • 苏州 网站的公司阿里云搜索
  • 顺德移动端网站建设公司网站推广费用
  • 找客户的软件宁波正规站内优化seo
  • 现在个人都在哪个网站做外贸seo权重优化
  • 网站开发为什么需要域名永州网站seo
  • 青岛开发区做网站设计的网站买卖交易平台
  • 家具行业网站整站模板酒店如何进行网络营销
  • 番禺网站建设平台东莞seo网站排名优化公司
  • 比分网站建设小璇seo优化网站
  • 杭州网站建设商城价格注册推广
  • 企业请别人做网站品牌广告语经典100条
  • php大气企业网站免费推广公司
  • 凡科做网站有什么用网络企业推广
  • 网站商城功能免费制作网站平台
  • 兼职 做网站创建网页
  • 凡科网做网站靠谱吗软件测试培训费用大概多少
  • 加若格网站做么样广州seo招聘信息
  • 公司注册资金查询站长之家seo概况查询