Skip to content

Commit ddd67f2

Browse files
committed
math/big: add examples for Rat and Int's SetString and Scan methods
R=golang-dev, bradfitz, rsc, r, gri, r CC=golang-dev https://golang.org/cl/5543047
1 parent e3ab30b commit ddd67f2

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/pkg/math/big/example_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2012 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.
4+
5+
package big_test
6+
7+
import (
8+
"fmt"
9+
"log"
10+
"math/big"
11+
)
12+
13+
// 3.142
14+
func ExampleRat_SetString() {
15+
r := new(big.Rat)
16+
r.SetString("355/113")
17+
fmt.Println(r.FloatString(3))
18+
}
19+
20+
// 420
21+
func ExampleInt_SetString() {
22+
i := new(big.Int)
23+
i.SetString("644", 8) // octal
24+
fmt.Println(i)
25+
}
26+
27+
// 3/2
28+
func ExampleRat_Scan() {
29+
// The Scan function is rarely used directly;
30+
// the fmt package recognizes it as an implementation of fmt.Scanner.
31+
r := new(big.Rat)
32+
_, err := fmt.Sscan("1.5000", r)
33+
if err != nil {
34+
log.Println("error scanning value:", err)
35+
} else {
36+
fmt.Println(r)
37+
}
38+
}
39+
40+
// 18446744073709551617
41+
func ExampleInt_Scan() {
42+
// The Scan function is rarely used directly;
43+
// the fmt package recognizes it as an implementation of fmt.Scanner.
44+
i := new(big.Int)
45+
_, err := fmt.Sscan("18446744073709551617", i)
46+
if err != nil {
47+
log.Println("error scanning value:", err)
48+
} else {
49+
fmt.Println(i)
50+
}
51+
}

0 commit comments

Comments
 (0)