golang 的那些花样

A Tour of Go 可以看到一些 Go 比较特殊的点

文章目录

变量声明时,类型放在后面

go 复制代码
var i, j int = 1, 2

declaration-syntax

Array 的引用 Slice

slices-intro

receiver 和 argument

receiver 习惯用于改变值,尤其是大的struct

go 复制代码
type Vertex struct {
	X, Y float64
}

func (v *Vertex) Scale(f float64) {
	v.X = v.X * f
	v.Y = v.Y * f
}

argument 就是method的参数,习惯用于业务逻辑

go 复制代码
type Vertex struct {
	X, Y float64
}

func AbsFunc(v Vertex) float64 {
	return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

receiver 用于实现接口,用于业务逻辑

go 复制代码
type Abser interface {
	Abs() float64
}

func main() {
	var a Abser
	v := Vertex{3, 4}

	a = &v // a *Vertex implements Abser

	fmt.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

go 复制代码
package main

import (
	"fmt"
	"math"
)

type ErrNegativeSqrt float64

func (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.5
		fmt.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)

go 复制代码
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

go 复制代码
	// 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 = true
			defer 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 = true
				defer p.catchPanic(p.arg, verb, "Error")
				p.fmtString(v.Error(), verb)
				return

			case Stringer:
				handled = true
				defer p.catchPanic(p.arg, verb, "String")
				p.fmtString(v.String(), verb)
				return
			}
		}
	}
	return false
}

A Tour of Go 习题解答

A Tour of Go 习题解答 2

学习字典,随时查

相关推荐
风逸hhh1 小时前
python打卡day46@浙大疏锦行
开发语言·python
火兮明兮1 小时前
Python训练第四十三天
开发语言·python
ascarl20102 小时前
准确--k8s cgroup问题排查
java·开发语言
fpcc3 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
莱茵菜苗3 小时前
Python打卡训练营day46——2025.06.06
开发语言·python
爱学习的小道长3 小时前
Python 构建法律DeepSeek RAG
开发语言·python
luojiaao4 小时前
【Python工具开发】k3q_arxml 简单但是非常好用的arxml编辑器,可以称为arxml杀手包
开发语言·python·编辑器
终焉代码4 小时前
STL解析——list的使用
开发语言·c++
SoFlu软件机器人4 小时前
智能生成完整 Java 后端架构,告别手动编写 ControllerServiceDao
java·开发语言·架构
英英_4 小时前
视频爬虫的Python库
开发语言·python·音视频