Middle
What can you say about operators in Go, have you used them at work?
sobes.tech AI
Answer from AI
In Go, operators are built-in symbols or keywords that perform basic data operations: arithmetic (+, -, *, /), logical (&&, ||, !), comparison (==, !=, <, >, <=, >=), as well as bitwise (&, |, ^, <<, >>).
Writing custom operators in Go is not possible — the language does not support operator overloading. However, you can implement methods for types to mimic operator behavior, for example, an Add method for adding structures.
At work, I used standard operators for data processing, and for more complex operations, I created functions and methods that provide the necessary logic. For example:
package main
import "fmt"
type Vector struct {
X, Y int
}
func (v Vector) Add(other Vector) Vector {
return Vector{v.X + other.X, v.Y + other.Y}
}
func main() {
v1 := Vector{1, 2}
v2 := Vector{3, 4}
v3 := v1.Add(v2)
fmt.Println(v3) // {4 6}
}
Thus, operators in Go are basic tools, and extendability is achieved through methods and functions.