当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > golang struct扩展函数参数命名警告解决方法

golang struct扩展函数参数命名警告解决方法

2017年12月08日  | 移动技术网IT编程  | 我要评论

今天在使用vscode编写golang代码时,定义一个struct,扩展几个方法,如下:

package storage

import (
  "fmt"

  "github.com/zsy619/gcommon"
)

//chunkfooter 块footer
type chunkfooter struct {
  chunkdatatotalsize int
}

//newchunkfooter 创建一个chunkfooter
func newchunkfooter(chunkdatatotalsize int) *chunkfooter {
  var result = new(chunkfooter)
  result.chunkdatatotalsize = chunkdatatotalsize
  return result
}

//tostring chunkfooter转换为string
func (cf *chunkfooter) tostring() string {
  return fmt.sprintf("[chunkdatatotalsize:%d]", cf.chunkdatatotalsize)
}

//asbytearray 转换成byte数组
func (nf *chunkfooter) asbytearray() []byte {
  //var result [chunkfootersize]byte
  buffer := gcommon.inttofixedlengthbytes(nf.chunkdatatotalsize, chunkfootersize)
  return buffer
}

请注意函数tostring与asbytearray中的*chunkfooter参数,一个是cf,一个nf,提示以下警告:

意思是将nf更改为cf,也就是struct扩展函数中对应对象的命名应该已第一个函数的命名为基准。

   可参考参官方指南和golang code review comments进行整理,力图与官方及社区编码风格保持一致。

   将函数tostring修改成如下:

//tostring chunkfooter转换为string
func (this *chunkfooter) tostring() string {
  return fmt.sprintf("[chunkdatatotalsize:%d]", this.chunkdatatotalsize)
}

提示以下警告信息:

要排除使用me、this、self这些命名。可根据golang官网的命名规则,并结合本公司要求,进行统一命名。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网