本文主要是介绍golang 的那些花样,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
从 A Tour of Go 可以看到一些 Go 比较特殊的点
文章目录
- 变量声明时,类型放在后面
- Array 的引用 Slice
- receiver 和 argument
- built-int特殊接口Error
变量声明时,类型放在后面
var i, j int = 1, 2
declaration-syntax
Array 的引用 Slice
slices-intro
receiver 和 argument
receiver 习惯用于改变值,尤其是大的struct
type Vertex struct {X, Y float64
}func (v *Vertex) Scale(f float64) {v.X = v.X * fv.Y = v.Y * f
}
argument 就是method的参数,习惯用于业务逻辑
type Vertex struct {X, Y float64
}func AbsFunc(v Vertex) float64 {return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
receiver 用于实现接口,用于业务逻辑
type Abser interface {Abs() float64
}func main() {var a Abserv := Vertex{3, 4}a = &v // a *Vertex implements Abserfmt.Println(a.Abs())
}type Vertex struct {X, Y float64
}func (v *Vertex) Abs() float64 {return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
built-int特殊接口Error
package mainimport ("fmt""math"
)type ErrNegativeSqrt float64func (x ErrNegativeSqrt) Error() string {return fmt.Sprintf("cannot Sqrt negative number: %f", x)
}func Sqrt2(x float64) (float64, error) {fmt.Println("processing.......")if x>0 {z := float64(1)z = 0.5fmt.Println(z)for i:=0; math.Abs(z*z -x)>0.00001; i++ {z = z - (z*z -x )/(2*z)fmt.Println("i", i, "z", z, "z*z -x", z*z -x)}fmt.Println("finished.")return z, nil} else {return 0, ErrNegativeSqrt(x)}}func main() {fmt.Println(Sqrt2(2))fmt.Println(Sqrt2(-2))
}
这里面如果在实现Error接口的时候调fmt.Sprintf用了%v
,会再次调x.Error()
进行无限循环, 如果用%v
的话先把值转换出来float64(x)
func (x ErrNegativeSqrt) Error() string {return fmt.Sprintf("cannot Sqrt negative number: %v", float64(x))
}
查看golang源码 https://cs.opensource.google/go/go/+/refs/tags/go1.17:src/fmt/print.go;l=617
// If we're doing Go syntax and the argument knows how to supply it, take care of it now.if p.fmt.sharpV {if stringer, ok := p.arg.(GoStringer); ok {handled = truedefer p.catchPanic(p.arg, verb, "GoString")// Print the result of GoString unadorned.p.fmt.fmtS(stringer.GoString())return}} else {// If a string is acceptable according to the format, see if// the value satisfies one of the string-valued interfaces.// Println etc. set verb to %v, which is "stringable".switch verb {case 'v', 's', 'x', 'X', 'q':// Is it an error or Stringer?// The duplication in the bodies is necessary:// setting handled and deferring catchPanic// must happen before calling the method.switch v := p.arg.(type) {case error:handled = truedefer p.catchPanic(p.arg, verb, "Error")p.fmtString(v.Error(), verb)returncase Stringer:handled = truedefer p.catchPanic(p.arg, verb, "String")p.fmtString(v.String(), verb)return}}}return false
}
A Tour of Go 习题解答
A Tour of Go 习题解答 2
学习字典,随时查
这篇关于golang 的那些花样的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!