高质量编程简介及编码规范|青训营笔记

71 阅读1分钟

高质量编程

高质量——正确可靠,简洁清晰

高质量编程简介

原则:

  • 简单性
  • 可读性
  • 生产力

编码规范——代码格式

  • 推荐使用gofmt自动格式化代码
  • 注释
  1. 适合注释公共符号
//Open opens the named file for reading.If successful,methods on
//the returned file can be used for reading;the associated file
//If there is an error,it will br of type *PathError.
func Open(name string) (*File,error) {
    return OpenFile(name,O_RDONLY,0)
}
  1. 适合注释实现过程
  switch resp/StatusCode{
  case 307,308:
      redirrectMethod = repMethod
      shouldRedirct = true 
      includeBody = true
  
      if ireq.GetBody == nil && ireq.outgoingLength() != 0{
          //we had a request body,and 307/308 require
          //re-sending it,but GetBody is not defined.So just
          //return this response to the user instead of an 
          //error, like we did in Go 1.7 and earlier.
          shouldRedirect = false
      }
  }

  1. 解释代码的限制条件
//LinmitReader returns a Reader that from r
//but stops with Eof after n bytes
//The underlying implementation is a *LimitedReader.
func LinmitReader(r Reader ,n int 64) Reader {return &LimitedReader{r,n} }