Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel,
n'hésitez pas à nous contacter.
ID: go-best-practices/invalid-seek-value
Language: Go
Severity: Info
Category: Best Practices
Description
In Go, it is recommended to pass the arguments to a function in the correct order, according to the function signature. This helps improve code readability, maintainability, and reduces the chances of errors.
The reason why we should avoid myValue.Seek(io.SeekStart, 0)
and use myValue.Seek(0, io.SeekStart)
instead is to follow the convention of passing the offset (or other numerical parameter) before the whence parameter.
The Seek
function in Go’s io
package takes two parameters: offset
and whence
. The offset
parameter represents the distance from the reference point, and the whence
parameter represents the reference point itself.
By following the convention of passing the offset before the whence parameter, the code becomes more consistent with Go’s standard library and promotes uniformity across different codebases.
This convention aligns with how other functions in the standard library, as well as commonly used idioms in Go, work. For instance, in the Seek
function’s counterpart, the Read
function, the offset is passed before the buffer parameter: Read(buffer []byte, offset int64)
By using myValue.Seek(0, io.SeekStart)
and myValue.Seek(10, myio.SeekCurrent)
, it better conveys the intention of the code and makes it easier for developers to understand the purpose of each parameter.
To summarize, it is good coding practice to pass function arguments in the correct order, according to the function signature and established conventions. In this case, it means passing the offset before the whence parameter, as in myValue.Seek(0, io.SeekStart)
and myValue.Seek(10, myio.SeekCurrent)
.
Non-Compliant Code Examples
func main () {
myValue.Seek(io.SeekStart, 0)
myValue.Seek(myio.SeekCurrent, 10)
}
Compliant Code Examples
func main () {
myValue.Seek(0, io.SeekStart)
myValue.Seek(10, myio.SeekCurrent)
}