English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Solution to golang struct extension function parameter naming warning

While using VSCode to write golang code today, I defined a struct, extended several methods, as follows:

package storage
import (
  "fmt"
  "github.com"/zsy619/gcommon
)
//ChunkFooter block footer
type ChunkFooter struct {
  ChunkDataTotalSize int
}
//NewChunkFooter creates a ChunkFooter
func NewChunkFooter(chunkDataTotalSize int) *ChunkFooter {
  var result = new(ChunkFooter)
  result.ChunkDataTotalSize = chunkDataTotalSize
  return result
}
//ToString Convert ChunkFooter to String
func (cf *ChunkFooter) ToString() string {
  return fmt.Sprintf("[ChunkDataTotalSize:%d]", cf.ChunkDataTotalSize)
}
//AsByteArray Convert to byte array
func (nf *ChunkFooter) AsByteArray() []byte {
  //var result [chunkFooterSize]byte
  buffer := gcommon.IntToFixedLengthBytes(nf.ChunkDataTotalSize, ChunkFooterSize)
  return buffer
}

Please note the function ToString and AsByteArray in*ChunkFooter parameter, one is cf, one is nf, prompt the following warning:

It means changing nf to cf, that is, the naming of the corresponding object in the struct extension function should be based on the name of the first function.

   You can refer to the official guidelinesEffective GolangandGolang Code Review CommentsStrive to keep consistent with the official and community coding styles.

   Organize the function ToString as follows:

//ToString Convert ChunkFooter to String
func (this *ChunkFooter) ToString() string {
  return fmt.Sprintf("[ChunkDataTotalSize:%d]", this.ChunkDataTotalSize)
}

Prompt the following warning information:

It is recommended to exclude the use of names such as me, this, and self. You can follow the naming conventions of the Golang official website and combine them with the company's requirements for unified naming.