golang代码规范和单元测试

代码规范

方便团队内部统一风格,提高代码可读性,统一性

命名规范

  1. 包名
    1. 尽量和目录名一致
    2. 采用有意义,简短
    3. 不要和标准库冲突
    4. 包名应该尽量全部小写
  1. 文件名
    1. 如果多个单词可以采用蛇形命名法
  1. 变量名
    1. 蛇形 不使用
    2. 驼峰 go    un  userName
    3. 专有名词全大写或者全小写
  1. 结构名
    1. 驼峰 首字母大写
  1. 接口命名
    1. 和结构体差不多
    2. 接口已er结尾
    3. IR
  1. 常量命名
    1. 全部大写,多个单词采用蛇形APP_VERSION

注释规范:

go提供两种注释:

  1. //适合单行注释
  2. 大段注释
  3. 变量后面加注释
  4. 包注释
  5. 接口注释
  6. 函数注释
  7. 代码逻辑注释

import注释

  1. go自带的包
  2. 第三方的包
  3. 自己内部的包

单元测试

单元测试命令:go test

go test命令是一个按照一定约定和组织的测试代码驱动程序。在包目录中,所有以_test.go为后缀的源码文件都会被go test运行

我们写的__test.go源码文件不用担心内容过多,因为go build命令不会将这些测试文件打包到最后的可执行文件

test文件有4类,Test开头的 功能测试

Benchmark开头的 性能测试

example 模糊测试

func TestAdd(t *testing.T) {

}

跳过耗时单元测试

func TestAdd2 (t *testing.T) {
	if testing.Short() {
		t.Skip("short 模式下跳过")
	}
	re := add(a:1,b:2)
	if re != 3 {
	t.Errorf("expect %d,actual %d")
}
}

go test -short

运行一个短的

基于表格的测试用例

func TestAdd(t *Testing.T) {
	var dataset = []struct{
	a int
	b int
	out int
	}{
	{a:1,b:2,out:3}
	{a:1,b:2,out:3}
	{a:1,b:2,out:3}
	{a:1,b:2,out:3}
	{a:1,b:2,out:3}
	}
	for _,value := range dataset {
	re := add(value.a,value.b)
	if re != value.out {
	t.Errorf("expect:%d,actual:%d",value.out,re)
	}
	}
}

性能测试

核心函数

func BenchmarAdd(bb *testing.B) {
	var a,b,c int
	a = 123
	b = 456
	c = 789
	
	for i:= 0;i< bb.N;i++ {
	 if actual := add(a,b);actual != c{
	fmt.Printf("%d+%d,except:%d,actual:%d",a,b,c,actual)
	}
	}
}

实例

对三种字符串的拼接方式进行性能测试

package light_edit

import (
	"fmt"
	"strconv"
	"strings"
	"testing"
)

const numbers = 10000

func BenchmarkStringBuilder(b *testing.B) {
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		var builder strings.Builder
		for j := 0; j < numbers; j++ {
			builder.WriteString(strconv.Itoa(j))
		}
		_ = builder.String()
	}
	b.StopTimer()
}

func BenchmarkStringAdd(b *testing.B) {
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		var str string
		for j := 0; j < numbers; j++ {
			str = str + strconv.Itoa(j)
		}

	}
	b.StopTimer()

}

func BenchmarkStringSprintf(b *testing.B) {
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		var str string
		for j := 0; j < numbers; j++ {
			str = fmt.Sprintf("%s%d", str, j)
		}
	}
	b.StopTimer()
}

相关推荐

  1. golang代码规范单元测试

    2024-01-27 09:56:01       51 阅读
  2. Golang 单元测试

    2024-01-27 09:56:01       54 阅读
  3. 单元测试集成测试

    2024-01-27 09:56:01       28 阅读
  4. golang中实现LRU-K算法(附带单元测试)

    2024-01-27 09:56:01       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-27 09:56:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-27 09:56:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-27 09:56:01       82 阅读
  4. Python语言-面向对象

    2024-01-27 09:56:01       91 阅读

热门阅读

  1. Python中的虚拟现实(VR)技术和应用

    2024-01-27 09:56:01       54 阅读
  2. forwardRef - React父组件控制子组件

    2024-01-27 09:56:01       48 阅读
  3. 领导力风格

    2024-01-27 09:56:01       50 阅读
  4. 【LeetCode-452】用最少数量的箭引爆气球(贪心)

    2024-01-27 09:56:01       61 阅读
  5. 前端工程化之:webpack1-5(配置文件)

    2024-01-27 09:56:01       63 阅读
  6. ·模板方法模式

    2024-01-27 09:56:01       50 阅读
  7. Redis面试题34

    2024-01-27 09:56:01       49 阅读
  8. 比特币 ETF 费用战蔓延至欧洲

    2024-01-27 09:56:01       56 阅读
  9. 如何保障主机安全

    2024-01-27 09:56:01       50 阅读
  10. Ubuntu 20.04 升级到 Ubuntu 22.04

    2024-01-27 09:56:01       31 阅读
  11. 一步步安装Ruby攻略

    2024-01-27 09:56:01       56 阅读
  12. ARP安全针对欺骗攻击的解决方案

    2024-01-27 09:56:01       51 阅读
  13. Delphi 7 IdHTTP POST 中文乱码得解决

    2024-01-27 09:56:01       50 阅读