db51ddab by Michael Hatch

GetInt(), GetInt8(), GetInt16(), GetInt32(), GetInt64() and Example tests

1 parent baf2c63d
...@@ -28,8 +28,8 @@ import ( ...@@ -28,8 +28,8 @@ import (
28 "strconv" 28 "strconv"
29 "strings" 29 "strings"
30 30
31 "github.com/astaxie/beego/context" 31 "github.com/mvpmvh/beego/context"
32 "github.com/astaxie/beego/session" 32 "github.com/mvpmvh/beego/session"
33 ) 33 )
34 34
35 //commonly used mime-types 35 //commonly used mime-types
...@@ -382,8 +382,37 @@ func (c *Controller) GetStrings(key string) []string { ...@@ -382,8 +382,37 @@ func (c *Controller) GetStrings(key string) []string {
382 return []string{} 382 return []string{}
383 } 383 }
384 384
385 // GetInt returns input value as int64. 385 // GetInt returns input as an int
386 func (c *Controller) GetInt(key string) (int64, error) { 386 func (c *Controller) GetInt(key string) (int, error) {
387 return strconv.Atoi(c.Ctx.Input.Query(key))
388 }
389
390 // GetInt8 return input as an int8
391 func (c *Controller) GetInt8(key string) (int8, error) {
392 i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 8)
393 i8 := int8(i64)
394
395 return i8, err
396 }
397
398 // GetInt16 returns input as an int16
399 func (c *Controller) GetInt16(key string) (int16, error) {
400 i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 16)
401 i16 := int16(i64)
402
403 return i16, err
404 }
405
406 // GetInt32 returns input as an int32
407 func (c *Controller) GetInt32(key string) (int32, error) {
408 i64, err := strconv.ParseInt(c.Ctx.Input.Query(key), 10, 32)
409 i32 := int32(i64)
410
411 return i32, err
412 }
413
414 // GetInt64 returns input value as int64.
415 func (c *Controller) GetInt64(key string) (int64, error) {
387 return strconv.ParseInt(c.Ctx.Input.Query(key), 10, 64) 416 return strconv.ParseInt(c.Ctx.Input.Query(key), 10, 64)
388 } 417 }
389 418
......
1 // Copyright 2014 beego Author. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package beego
16
17 import (
18 "fmt"
19 "github.com/mvpmvh/beego/context"
20 )
21
22 func ExampleGetInt() {
23
24 i := &context.BeegoInput{Params: map[string]string{"age": "40"}}
25 ctx := &context.Context{Input: i}
26 ctrlr := Controller{Ctx: ctx}
27
28 val, _ := ctrlr.GetInt("age")
29 fmt.Printf("%T", val)
30 //Output: int
31 }
32
33 func ExampleGetInt8() {
34
35 i := &context.BeegoInput{Params: map[string]string{"age": "40"}}
36 ctx := &context.Context{Input: i}
37 ctrlr := Controller{Ctx: ctx}
38
39 val, _ := ctrlr.GetInt8("age")
40 fmt.Printf("%T", val)
41 //Output: int8
42 }
43
44 func ExampleGetInt16() {
45
46 i := &context.BeegoInput{Params: map[string]string{"age": "40"}}
47 ctx := &context.Context{Input: i}
48 ctrlr := Controller{Ctx: ctx}
49
50 val, _ := ctrlr.GetInt16("age")
51 fmt.Printf("%T", val)
52 //Output: int16
53 }
54
55 func ExampleGetInt32() {
56
57 i := &context.BeegoInput{Params: map[string]string{"age": "40"}}
58 ctx := &context.Context{Input: i}
59 ctrlr := Controller{Ctx: ctx}
60
61 val, _ := ctrlr.GetInt32("age")
62 fmt.Printf("%T", val)
63 //Output: int32
64 }
65
66 func ExampleGetInt64() {
67
68 i := &context.BeegoInput{Params: map[string]string{"age": "40"}}
69 ctx := &context.Context{Input: i}
70 ctrlr := Controller{Ctx: ctx}
71
72 val, _ := ctrlr.GetInt64("age")
73 fmt.Printf("%T", val)
74 //Output: int64
75 }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!