1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func TestNewEqual(t
*testing
.T
) {
14 // Different allocations should not be equal.
15 if errors
.New("abc") == errors
.New("abc") {
16 t
.Errorf(`New("abc") == New("abc")`)
18 if errors
.New("abc") == errors
.New("xyz") {
19 t
.Errorf(`New("abc") == New("xyz")`)
22 // Same allocation should be equal to itself (not crash).
23 err
:= errors
.New("jkl")
25 t
.Errorf(`err != err`)
29 func TestErrorMethod(t
*testing
.T
) {
30 err
:= errors
.New("abc")
31 if err
.Error() != "abc" {
32 t
.Errorf(`New("abc").Error() = %q, want %q`, err
.Error(), "abc")
37 err
:= errors
.New("emit macho dwarf: elf header corrupted")
41 // Output: emit macho dwarf: elf header corrupted
44 // The fmt package's Errorf function lets us use the package's formatting
45 // features to create descriptive error messages.
46 func ExampleNew_errorf() {
47 const name
, id
= "bimmler", 17
48 err
:= fmt
.Errorf("user %q (id %d) not found", name
, id
)
52 // Output: user "bimmler" (id 17) not found