这是我参与[第五届青训营]笔记创作活动的第2天。本文主要想对课堂上一些知识点进行记录总结。今天学习了高质量编程和性能调优。
一、本堂课重点内容:
- 高质量编程及编码规范
- 性能优化
二、详细知识点介绍:
1. 高质量编程及编码规范
高质量代码
-
对各种边界条件有着完备的考虑
-
能处理异常情况,保证稳定性
-
易读易维护
高质量go代码1.代码格式:可以使用gofmt自动格式化代码,将go语言代码格式化为官方统一风格。也可以使用goimports,等于gofmt加上依赖包管理等。
- 注释:注释应解释代码作用,解释代码如何做,解释代码实现的原因,解释代码什么情况会出错。注释应该提供代码未表达出的上下文信息。
// ParseInt interprets a string s in the given base (0, 2 to 36) and
// bit size (0 to 64) and returns the corresponding value i.
//
// The string may begin with a leading sign: "+" or "-".
//
// If the base argument is 0, the true base is implied by the string's
// prefix following the sign (if present): 2 for "0b", 8 for "0" or "0o",
// 16 for "0x", and 10 otherwise. Also, for argument base 0 only,
// underscore characters are permitted as defined by the Go syntax for
// [integer literals].
//
// The bitSize argument specifies the integer type
// that the result must fit into. Bit sizes 0, 8, 16, 32, and 64
// correspond to int, int8, int16, int32, and int64.
// If bitSize is below 0 or above 64, an error is returned.
//
// The errors that ParseInt returns have concrete type *NumError
// and include err.Num = s. If s is empty or contains invalid
// digits, err.Err = ErrSyntax and the returned value is 0;
// if the value corresponding to s cannot be represented by a
// signed integer of the given size, err.Err = ErrRange and the
// returned value is the maximum magnitude integer of the
// appropriate bitSize and sign.
//
// [integer literals]: https://go.dev/ref/spec#Integer_literals
func ParseInt(s string, base int, bitSize int) (i int64, err error)
3.命名规范:简洁而又能表达其含义。
4.控制流程:线性原则,减少嵌套使流程更清晰,代码更有可维护性和可读性。
2. 性能优化
待学习补充