Go 我怎样才能在“中”之间做出选择;“更新方法”;及;设置某物“;当命名成员方法时?
我发现至少有三个worldGo 我怎样才能在“中”之间做出选择;“更新方法”;及;设置某物“;当命名成员方法时?,go,naming-conventions,Go,Naming Conventions,我发现至少有三个worldSetTh,updateSth和updateSth出现在我的代码中,当我命名一个方法时,该方法的功能是设置/更新此对象的状态 我对这个选择感到困惑。 我搜索了golang项目的源代码,发现有许多源代码使用了setth 0项使用“changeSth” 8项使用“updateSth” 214项使用“设置值” net/fd_plan9.go:214:func(fd*netFD)setReadDeadline(t time.time) 错误{net/fd_plan9.go:
SetTh
,updateSth
和updateSth
出现在我的代码中,当我命名一个方法时,该方法的功能是设置/更新此
对象的状态
我对这个选择感到困惑。
我搜索了golang项目的源代码,发现有许多源代码使用了setth
- 0项使用“changeSth”
- 8项使用“updateSth”
- 214项使用“设置值” net/fd_plan9.go:214:func(fd*netFD)setReadDeadline(t time.time) 错误{net/fd_plan9.go:218:func(fd*netFD)setwritedadline(t time.time)错误{mime/multipart/writer.go:43:func(w*writer) SetBoundary(边界字符串)错误{log/log.go:218:func(l*记录器) SetFlags(flag int){log/log.go:232:func(l*Logger)SetPrefix(prefix 字符串){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)集合(值int64){ expvar/expvar.go:86:func(v*Float)集合(值float64){ expvar/expvar.go:147:func(v*Map)集合(键字符串,av变量){ expvar/expvar.go:228:func(v*String)集合(值字符串){ testing/benchmark.go:97:func(b*b)SetBytes(n int64){b.bytes=n} 测试/基准.go:430:func(b*b)SetParallelism(p int){ image/image.go:92:func(p*RGBA)集(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)集(x,y int,c color.color){
- 当您开始在包中使用“
”时,请坚持使用它updatexx()
- 如果您使用的是
,情况也一样SetXXX()
Get
和Set
在围棋中不是惯用的。当然,如果需要,setter函数可能会被称为SetXXX()
,如“。另一方面,
Update()
,可能有特定的用途
注意:在go源代码中找到的“
update()
”方法均未导出(公共).所以设置
和更新
之间没有本质的区别?使用哪一个仅仅由一致性或语言决定?@snyh一般来说,更新
如果你想更改已经设置的内容。设置
用于设置值,而不管它是否已经设置。最后的好奇。Doeschangesh
是完全错误的吗?我过去经常选择这个:(@snyh只要你不把更改和更新混为一谈,这意味着只要你选择一个约定并始终如一地使用它,更改就没有错()。
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) {