博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Go 若干技巧
阅读量:7096 次
发布时间:2019-06-28

本文共 2384 字,大约阅读时间需要 7 分钟。

此文来自 http://denvergophers.com/2013-09/tips-and-tricks.slide ###本文主要涉及到: 1. formatting 技巧 2. 异常处理技巧 3. 函数返回值的一致性 ###代码资源: https://denvergophers.com/tips-and-tricks http://golang.org/pkg/fmt http://godoc.org/code.google.com/p/go.tools/present ## fmt包 使用如下格式导入: import "fmt" 普通占位符: %v 相应值的默认格式 %+v 在打印结构体时,会添加字段名 %#v 相应值的Go语法表示 %T 相应值的类型的Go语法表示 %% 字面上的百分号,并非值的占位符 ### fmt一般用法 - 简单字符串 var foo string = "This is a simple string" fmt.Printf("%v\n", foo) fmt.Printf("%T\n", foo) ### fmt一般用法 - 结构(struct) 首先,准备好结构 type ( Customer struct { Name string Street []string City string State string Zip string } Item struct { Id int Name string Quantity int } Items []Item Order struct { Id int Customer Customer Items Items } ) 关于结构格式化的一些技巧: // 这是我调试时的默认格式 fmt.Printf("%+v\n\n", order) // 当我需要知道这个变量的有关结构时我会用这种方法 fmt.Printf("%#v\n\n", order) // 我很少使用这些 fmt.Printf("%v\n\n", order) fmt.Printf("%s\n\n", order) fmt.Printf("%T\n", order) ### fmt - 使用errors.New()生成Errors 这是我最不喜欢看到的创建异常的方式: import ( "errors" "fmt" "log" ) func main() { if err := iDunBlowedUp(-100); err != nil { err = errors.New(fmt.Sprintf("Something went wrong: %s\n", err)) log.Println(err) return } fmt.Printf("Success!") } func iDunBlowedUp(val int) error { return errors.New(fmt.Sprintf("invalid value %d", val)) } 我是这么创建异常的: import ( "fmt" "log" ) func main() { if err := iDunBlowedUp(-100); err != nil { err = fmt.Errorf("Something went wrong: %s\n", err) log.Println(err) return } fmt.Printf("Success!") } func iDunBlowedUp(val int) error { return fmt.Errorf("invalid value %d", val) } ### fmt - 函数返回值的一致性 坏习惯: func someFunction(val int) (ok bool, err error) { if val == 0 { return false, nil } if val < 0 { return false, fmt.Errorf("value can't be negative %d", val) } ok = true return } 好习惯: func someFunction(val int) (bool, error) { if val == 0 { return false, nil } if val < 0 { return false, fmt.Errorf("value can't be negative %d", val) } return true, nil } 更好的方式(在我看来): func someFunction(val int) (ok bool, err error) { if val == 0 { return } if val < 0 { err = fmt.Errorf("value can't be negative %d", val) return } ok = true return } ## 参考 http://golang.org/ https://denvergophers.com/tips-and-tricks http://golang.org/pkg/fmt http://godoc.org/code.google.com/p/go.tools/present ## 作者相关 https://github.com/DenverGophers https://twitter.com/DenverGophers https://plus.google.com/u/0/communities/104822260820066412402

转载地址:http://jcxql.baihongyu.com/

你可能感兴趣的文章
HTML5/CSS3系列教程:HTML5 区域(Sectioning)的重要性
查看>>
Spring Batch学习笔记
查看>>
asp.net mvc 如何在执行完某任务后返回原来页面
查看>>
Oracle: listener.ora 、sqlnet.ora 、tnsnames.ora的配置及例子
查看>>
ASP.NET 中 GridView(网格视图)的使用前台绑定
查看>>
Windows的本地时间(LocalTime)、系统时间(SystemTime)、格林威治时间(UTC-Time)、文件时间(FileTime)之间的转换...
查看>>
[转]XBRL应用软件分类
查看>>
C++ 文件的复制、删除、重命名
查看>>
Oracle Patch Set Update and Critical Patch Update April 2011 Released
查看>>
hdu 2189
查看>>
std::map, std::multimap, std::tr1::unordered_map 区别 - 笔记本 - 博客频道 - CSDN.NET
查看>>
/usr/bin/ld: cannot find -lxxx问题总结
查看>>
C 语言 restrict 关键字的使用
查看>>
ASP.NET 自定义成员资格提供程序 Part.4(使用自定义提供程序类)
查看>>
ASP.NET调用V3版本的Google Maps API
查看>>
苹果面试8大难题及答案
查看>>
运行 tomcat 错误 : java.lang.Exception: Socket bind failed: [730048] 解决方法:
查看>>
并行接口和串行接口
查看>>
Adding a nested ESXi on 5.1
查看>>
类型函数C语言void关键字
查看>>