I have found at least three world setSth
,updateSth
and updateSth
appear in my code when I naming a method whose function is set/update the state of this
object.
I'm confuse this choosing. I search source code of golang project and found there have many one using setSth
.
0 items using "changeSth"
8 items using "updateSth"
text/tabwriter/tabwriter.go 380:func (b *Writer) updateWidth() { go/ast/commentmap.go 246:func (cmap CommentMap) Update(old, new Node) Node { container/heap/example_pq_test.go 54:func (pq *PriorityQueue) update(item *Item, value string, priority int) { expvar/expvar.go 129:func (v *Map) updateKeys() { encoding/gob/encode.go 101:func (state *encoderState) update(instr *encInstr) { crypto/cipher/gcm.go 236:func (g *gcm) updateBlocks(y *gcmFieldElement, blocks []byte) { 247:func (g *gcm) update(y *gcmFieldElement, data []byte) { index/suffixarray/qsufsort.go 145:func (x *suffixSortable) updateGroups(offset int) {
214 items using "setSth"
net/fd_plan9.go:214:func (fd *netFD) setReadDeadline(t time.Time)
error { net/fd_plan9.go:218:func (fd *netFD) setWriteDeadline(t
time.Time) error { mime/multipart/writer.go:43:func (w *Writer)
SetBoundary(boundary string) error { log/log.go:218:func (l *Logger)
SetFlags(flag int) { log/log.go:232:func (l *Logger) SetPrefix(prefix
string) { bufio/scan.go:198:func (s *Scanner) setErr(err error) {
archive/zip/struct.go:185:func (h *FileHeader) SetModTime(t time.Time)
{ archive/zip/struct.go:223:func (h *FileHeader) SetMode(mode
os.FileMode) { expvar/expvar.go:60:func (v *Int) Set(value int64) {
expvar/expvar.go:86:func (v *Float) Set(value float64) {
expvar/expvar.go:147:func (v *Map) Set(key string, av Var) {
expvar/expvar.go:228:func (v *String) Set(value string) {
testing/benchmark.go:97:func (b *B) SetBytes(n int64) { b.bytes = n }
testing/benchmark.go:430:func (b *B) SetParallelism(p int) {
image/image.go:92:func (p *RGBA) Set(x, y int, c color.Color) {
image/image.go:104:func (p *RGBA) SetRGBA(x, y int, c color.RGBA) {
image/image.go:196:func (p *RGBA64) Set(x, y int, c color.Color) {
The key remains consistency:
UpdateXXX()
" in a package, stick with it.SetXXX()
.That being said, Get
and Set
are not idiomatic in Go. Certainly not for a Getter, while a setter function, if needed, will likely be called SetXXX()
, as in "how to set and get fields in golang structs?".Update()
, on the other hand, might have a specific purpose.
Note: none of the "update()
" methods you have found in the go source are exported (public).