unknwon/the-way-to-go_ZH_CN · info

math - square root of negative number

Error message

math - square root of negative number

What it means

This is documentation text, not shipped library code: section 13.1 of the Go eBook introduces error values with err := errors.New("math - square root of negative number"). The message is the book's canonical example of a descriptive error string for an invalid-domain condition; errors.New allocates an errorString carrying exactly that text.

Source

Thrown at eBook/13.1.md:18

# 13.1 错误处理

Go 有一个预先定义的 error 接口类型

```go
type error interface {
	Error() string
}
```

错误值用来表示异常状态;我们可以在 [5.2 节](05.2.md)中看到它的标准用法。处理文件操作的例子可以在 [12 章](12.0.md)找到;我们将在 [15 章](15.0.md)看到网络操作的例子。`errors` 包中有一个 `errorString` 结构体实现了 `error` 接口。当程序处于错误状态时可以用 `os.Exit(1)` 来中止运行。

## 13.1.1 定义错误

任何时候当你需要一个新的错误类型,都可以用 `errors` 包(必须先 `import`)的 `errors.New()` 函数接收合适的错误信息来创建,像下面这样:

```go
err := errors.New("math - square root of negative number")
```

在示例 13.1 中你可以看到一个简单的用例:

示例 13.1 [errors.go](examples/chapter_13/errors.go):

```go
// errors.go
package main

import (
	"errors"
	"fmt"
)

var errNotFound error = errors.New("Not found error")

func main() {

View on GitHub (pinned to 7a54d34d36)

Solutions

  1. Treat the snippet as a template: keep the shape, rewrite the text for your domain, e.g. errors.New("geometry: square root of negative number")
  2. If you pasted the one-liner verbatim, fix the compile error by using err (return it, log it) or discarding with _ = err while experimenting
  3. If you already shipped this exact text and match on it, replace string comparison with a package-level sentinel: var ErrNegativeSqrt = errors.New(...) plus errors.Is
  4. Run go vet / staticcheck after pasting snippets to catch unused imports (errors) and unused variables

Example fix

// before (copied verbatim; also fails to compile: err declared and not used)
err := errors.New("math - square root of negative number")

// after (package-level sentinel, prefix matches your package, comparable)
var ErrNegativeSqrt = errors.New("geometry: square root of negative number")

func Sqrt(f float64) (float64, error) {
	if f < 0 {
		return 0, fmt.Errorf("Sqrt(%g): %w", f, ErrNegativeSqrt)
	}
	return math.Sqrt(f), nil
}
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Nothing in the repo produces it at runtime — the string only exists in a Markdown code fence. You hit it by copying the snippet into your own Sqrt-like function and calling it with a negative argument, or by grepping for error idioms and landing here.

Common situations: Readers pasting book snippets into real code and later finding this exact text in logs; tests asserting on the exact message string; project renames where the 'math -' prefix no longer matches the actual package name; noticing the snippet as printed does not compile standalone (err is declared and not used).

Related errors


AI-assisted analysis of unknwon/the-way-to-go_ZH_CN@7a54d34d36 (2026-08-15). Data as JSON: /api/errors/1d54f98361ad7485. Report an issue: GitHub.