* gcc.dg/ipa/inlinehint-4.c: Also pass --param inline-unit-growth=20.
[official-gcc.git] / libgo / go / strconv / example_test.go
blob01fbbc0fb9eb59df0e06e5b5479693cd1239efa4
1 // Copyright 2015 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.
5 package strconv_test
7 import (
8 "fmt"
9 "log"
10 "strconv"
13 func ExampleAppendBool() {
14 b := []byte("bool:")
15 b = strconv.AppendBool(b, true)
16 fmt.Println(string(b))
18 // Output:
19 // bool:true
22 func ExampleAppendFloat() {
23 b32 := []byte("float32:")
24 b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32)
25 fmt.Println(string(b32))
27 b64 := []byte("float64:")
28 b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64)
29 fmt.Println(string(b64))
31 // Output:
32 // float32:3.1415927E+00
33 // float64:3.1415926535E+00
36 func ExampleAppendInt() {
37 b10 := []byte("int (base 10):")
38 b10 = strconv.AppendInt(b10, -42, 10)
39 fmt.Println(string(b10))
41 b16 := []byte("int (base 16):")
42 b16 = strconv.AppendInt(b16, -42, 16)
43 fmt.Println(string(b16))
45 // Output:
46 // int (base 10):-42
47 // int (base 16):-2a
50 func ExampleAppendQuote() {
51 b := []byte("quote:")
52 b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`)
53 fmt.Println(string(b))
55 // Output:
56 // quote:"\"Fran & Freddie's Diner\""
59 func ExampleAppendQuoteRune() {
60 b := []byte("rune:")
61 b = strconv.AppendQuoteRune(b, '☺')
62 fmt.Println(string(b))
64 // Output:
65 // rune:'☺'
68 func ExampleAppendQuoteRuneToASCII() {
69 b := []byte("rune (ascii):")
70 b = strconv.AppendQuoteRuneToASCII(b, '☺')
71 fmt.Println(string(b))
73 // Output:
74 // rune (ascii):'\u263a'
77 func ExampleAppendQuoteToASCII() {
78 b := []byte("quote (ascii):")
79 b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`)
80 fmt.Println(string(b))
82 // Output:
83 // quote (ascii):"\"Fran & Freddie's Diner\""
86 func ExampleAppendUint() {
87 b10 := []byte("uint (base 10):")
88 b10 = strconv.AppendUint(b10, 42, 10)
89 fmt.Println(string(b10))
91 b16 := []byte("uint (base 16):")
92 b16 = strconv.AppendUint(b16, 42, 16)
93 fmt.Println(string(b16))
95 // Output:
96 // uint (base 10):42
97 // uint (base 16):2a
100 func ExampleAtoi() {
101 v := "10"
102 if s, err := strconv.Atoi(v); err == nil {
103 fmt.Printf("%T, %v", s, s)
106 // Output:
107 // int, 10
110 func ExampleCanBackquote() {
111 fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺"))
112 fmt.Println(strconv.CanBackquote("`can't backquote this`"))
114 // Output:
115 // true
116 // false
119 func ExampleFormatBool() {
120 v := true
121 s := strconv.FormatBool(v)
122 fmt.Printf("%T, %v\n", s, s)
124 // Output:
125 // string, true
128 func ExampleFormatFloat() {
129 v := 3.1415926535
131 s32 := strconv.FormatFloat(v, 'E', -1, 32)
132 fmt.Printf("%T, %v\n", s32, s32)
134 s64 := strconv.FormatFloat(v, 'E', -1, 64)
135 fmt.Printf("%T, %v\n", s64, s64)
137 // Output:
138 // string, 3.1415927E+00
139 // string, 3.1415926535E+00
142 func ExampleFormatInt() {
143 v := int64(-42)
145 s10 := strconv.FormatInt(v, 10)
146 fmt.Printf("%T, %v\n", s10, s10)
148 s16 := strconv.FormatInt(v, 16)
149 fmt.Printf("%T, %v\n", s16, s16)
151 // Output:
152 // string, -42
153 // string, -2a
156 func ExampleFormatUint() {
157 v := uint64(42)
159 s10 := strconv.FormatUint(v, 10)
160 fmt.Printf("%T, %v\n", s10, s10)
162 s16 := strconv.FormatUint(v, 16)
163 fmt.Printf("%T, %v\n", s16, s16)
165 // Output:
166 // string, 42
167 // string, 2a
170 func ExampleIsPrint() {
171 c := strconv.IsPrint('\u263a')
172 fmt.Println(c)
174 bel := strconv.IsPrint('\007')
175 fmt.Println(bel)
177 // Output:
178 // true
179 // false
182 func ExampleItoa() {
183 i := 10
184 s := strconv.Itoa(i)
185 fmt.Printf("%T, %v\n", s, s)
187 // Output:
188 // string, 10
191 func ExampleParseBool() {
192 v := "true"
193 if s, err := strconv.ParseBool(v); err == nil {
194 fmt.Printf("%T, %v\n", s, s)
197 // Output:
198 // bool, true
201 func ExampleParseFloat() {
202 v := "3.1415926535"
203 if s, err := strconv.ParseFloat(v, 32); err == nil {
204 fmt.Printf("%T, %v\n", s, s)
206 if s, err := strconv.ParseFloat(v, 64); err == nil {
207 fmt.Printf("%T, %v\n", s, s)
210 // Output:
211 // float64, 3.1415927410125732
212 // float64, 3.1415926535
215 func ExampleParseInt() {
216 v32 := "-354634382"
217 if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
218 fmt.Printf("%T, %v\n", s, s)
220 if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
221 fmt.Printf("%T, %v\n", s, s)
224 v64 := "-3546343826724305832"
225 if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
226 fmt.Printf("%T, %v\n", s, s)
228 if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
229 fmt.Printf("%T, %v\n", s, s)
232 // Output:
233 // int64, -354634382
234 // int64, -3546343826724305832
237 func ExampleParseUint() {
238 v := "42"
239 if s, err := strconv.ParseUint(v, 10, 32); err == nil {
240 fmt.Printf("%T, %v\n", s, s)
242 if s, err := strconv.ParseUint(v, 10, 64); err == nil {
243 fmt.Printf("%T, %v\n", s, s)
246 // Output:
247 // uint64, 42
248 // uint64, 42
251 func ExampleQuote() {
252 s := strconv.Quote(`"Fran & Freddie's Diner ☺"`)
253 fmt.Println(s)
255 // Output:
256 // "\"Fran & Freddie's Diner\t☺\""
259 func ExampleQuoteRune() {
260 s := strconv.QuoteRune('☺')
261 fmt.Println(s)
263 // Output:
264 // '☺'
267 func ExampleQuoteRuneToASCII() {
268 s := strconv.QuoteRuneToASCII('☺')
269 fmt.Println(s)
271 // Output:
272 // '\u263a'
275 func ExampleQuoteToASCII() {
276 s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`)
277 fmt.Println(s)
279 // Output:
280 // "\"Fran & Freddie's Diner\t\u263a\""
283 func ExampleUnquote() {
284 test := func(s string) {
285 t, err := strconv.Unquote(s)
286 if err != nil {
287 fmt.Printf("Unquote(%#v): %v\n", s, err)
288 } else {
289 fmt.Printf("Unquote(%#v) = %v\n", s, t)
293 s := `\"Fran & Freddie's Diner\t\u263a\"\"`
294 // If the string doesn't have quotes, it can't be unquoted.
295 test(s) // invalid syntax
296 test("`" + s + "`")
297 test(`"` + s + `"`)
298 test(`'\u263a'`)
300 // Output:
301 // Unquote("\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\""): invalid syntax
302 // Unquote("`\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\"`") = \"Fran & Freddie's Diner\t\u263a\"\"
303 // Unquote("\"\\\"Fran & Freddie's Diner\\t\\u263a\\\"\\\"\"") = "Fran & Freddie's Diner ☺""
304 // Unquote("'\\u263a'") = ☺
307 func ExampleUnquoteChar() {
308 v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
309 if err != nil {
310 log.Fatal(err)
313 fmt.Println("value:", string(v))
314 fmt.Println("multibyte:", mb)
315 fmt.Println("tail:", t)
317 // Output:
318 // value: "
319 // multibyte: false
320 // tail: Fran & Freddie's Diner\"
323 func ExampleNumError() {
324 str := "Not a number"
325 if _, err := strconv.ParseFloat(str, 64); err != nil {
326 e := err.(*strconv.NumError)
327 fmt.Println("Func:", e.Func)
328 fmt.Println("Num:", e.Num)
329 fmt.Println("Err:", e.Err)
330 fmt.Println(err)
333 // Output:
334 // Func: ParseFloat
335 // Num: Not a number
336 // Err: invalid syntax
337 // strconv.ParseFloat: parsing "Not a number": invalid syntax