Go- 包的使用
发布于: 3 小时前

Go 学习笔记,学习内容《Go入门指南》
主要介绍以下内容:
regexp 包
sync 包
big 包
Go 包检索地址:gowolker 包含每个包的函数说明和使用方法
代码示例可以直接运行
package main
import ( "fmt" "math" "math/big" "regexp" "strconv")
func printDivider(str ...string) { fmt.Println() fmt.Println("-----------------------------------------------") if len(str) > 0 { fmt.Println(str) }}
func main() { /* regexp包 */ printDivider("regexp包") searchIn := "Scores about John: 283.1001 88.2" pat := "[0-9]+.[0-9]+"
if ok, _ := regexp.Match(pat, []byte(searchIn)); ok { // 查找指定格式是否存在 fmt.Println("Match Found") // 输出:Match Found }
f := func(s string) string { v, _ := strconv.ParseFloat(s, 32) return strconv.FormatFloat(v*2, 'f', 2, 32) }
re, _ := regexp.Compile(pat) str1 := re.ReplaceAllString(searchIn, "##.#") // 将指定模式的字符串全部替换 fmt.Println(str1) // 输出:Scores about John: ##.# ##.#
str2 := re.ReplaceAllStringFunc(searchIn, f) // 匹配模式的字符串作为参数,用函数返回值替换字符串 fmt.Println(str2) // 输出:Scores about John: 566.20 176.40
/* sync包 1、加锁操作:通过加锁操作实现资源的互斥和原子操作 https://gowalker.org/sync#Mutex 2、函数的互斥调用:once.Do(call) https://gowalker.org/sync#Once_Do */
/* big包 float64精确到小数点之后15位 */ im := big.NewInt(math.MaxInt64) in := im io := big.NewInt(1984) ip := big.NewInt(1) fmt.Println(im, in, io, ip) // 输出:9223372036854775807 9223372036854775807 1984 1
num1 := big.NewInt(1) num2 := big.NewInt(2) num3 := big.NewInt(3) num4 := big.NewInt(100) fmt.Println(num1.Mul(num2, num3)) // num1 = num2 * num3 输出:6 fmt.Println(num1) // 输出:6
fmt.Println(num1.Mul(num2, num3).Add(num1, num4)) // num1 = num2 * num3; num1 = num1 + num4; 输出:106 fmt.Println(num1) // 输出:106}复制代码
划线
评论
复制
发布于: 3 小时前阅读数: 2
版权声明: 本文为 InfoQ 作者【HelloBug】的原创文章。
原文链接:【http://xie.infoq.cn/article/5ee3b20d3a90409b94510ef90】。
本文遵守【CC BY-NC-ND】协议,转载请保留原文出处及本版权声明。
HelloBug
关注
还未添加个人签名 2018.09.20 加入
还未添加个人简介











评论