代码规范

格式规范

golang在代码格式方面比较统一,并提供了gofmt工具,所有代码经过gofmt格式化后,能达到统一风格。行长建议不要超过小屏幕的2/3,不要超过大屏幕的1/2, 函数长度不超过一屏

命名

     1、变量名&类名&接口名
     1、变量名&类名&接口名
            【强制】变量类名使用单词或驼峰结构
            【强制】创建类函数使用New/new + 类名
            【强制】函数参数以小写字母开头                                                  
      如:  var CandidateSize int //公共变量名
              var candidateSize int //私有变量名

     2、包名&目录名
          【建议】简短
          【强制】统一小写,无“_”
          【强制】包名目录名一致

     3、文件名
         【强制】小写单词 或 小写 + “_” 组合

import 导入

        【强制】导入次序:系统库 – 开源库 – 内部公共库 – 项目包
         【强制】禁止使用 . 导入
         如:                  
         //导入次序
import (
"encoding/xml"
"flag"
"fmt"
"os"
"os/signal"
"syscall"

log "github.com/cihub/seelog"

"innotech.com/qutoutiao/lib/client"
"innotech.com/qutoutiao/lib/config"
"innotech.com/qutoutiao/lib/datasource"
"innotech.com/qutoutiao/lib/httpserver"
"innotech.com/qutoutiao/lib/prometheusexport"
"innotech.com/qutoutiao/lib/ratelimit"
"innotech.com/qutoutiao/lib/utils"

"innotech.com/qutoutiao/engine/document"
"innotech.com/qutoutiao/engine/httphandler"
"innotech.com/qutoutiao/engine/recommender"
"innotech.com/qutoutiao/engine/strategy"
)
 //禁止方式
import (
     . "innotech.com/qutoutiao/engine/appconfig"
 )

编码原则

               【建议】map访问判断key是否存在,v, ok := map[k]

               【建议】尽量不使用panic,而是以错误形式返回异常

               【强制】函数如果返回 error,bool 等,放在参数最后一位

               【建议】public接口函数进行参数检查,特别是对指针类型

               【建议】每个函数内defer不宜过多

               【强制】不在循环里使用defer