Documentation
¶
Overview ¶
Package utils is a packages of some helper functions
See README.md for more info https://github.com/zcong1993/utils.
Index ¶
- func Compile(w io.Writer, tpl string, data interface{}) error
- func CompileText(w io.Writer, tpl string, data interface{}) error
- func GetJSON(url string, v interface{}) error
- func GetJSONWithHeaders(url string, v interface{}, headers map[string]string) error
- func PostJSON(url string, body interface{}, v interface{}, headers map[string]string) error
- func SliceIndex(limit int, condition func(num int) bool) int
- func StringAddress(v string) *string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compile ¶
Compile is a html template compiler with custom tpl and data
Example ¶
Example compile inline template with data, put result to io.Write you defined
package main
import (
"github.com/zcong1993/utils"
"os"
)
func main() {
tpl := "hello {{.data}}"
data := map[string]string{"data": "world"}
err := utils.Compile(os.Stdout, tpl, &data)
// check error
if err != nil {
panic(err)
}
}
Output: hello world
func CompileText ¶
CompileText is same as Compile but use text/template
func GetJSON ¶
GetJSON make a get request use http.Get
Example ¶
Example (simple) make a simple http get request for json data
package main
import (
"fmt"
"github.com/zcong1993/utils"
)
func main() {
// define a response data type
type Resp struct {
Status int `decoder:"status"`
Msg string `decoder:"msg"`
}
var res Resp
// this api is always return json data {"status":200, "msg": "hello world}
err := utils.GetJSON("http://zcong-hello.getsandbox.com/hello", &res)
// check error
if err != nil {
panic(err)
}
// res is response json data
fmt.Printf("%+v", res)
}
Output: {Status:200 Msg:hello world}
func GetJSONWithHeaders ¶
GetJSONWithHeaders make a http get request with custom headers
Example ¶
Example (custom headers) can add some custom headers when request
package main
import (
"fmt"
"github.com/zcong1993/utils"
)
func main() {
// define a response data type
type Header struct {
Name string `decoder:"name"`
Value string `decoder:"value"`
}
type Headers struct {
Headers []Header `decoder:"headers"`
}
// define some custom headers
customHeaders := map[string]string{"Foo": "bar"}
var res Headers
// this api return all the request headers
err := utils.GetJSONWithHeaders("http://zcong-hello.getsandbox.com/header", &res, customHeaders)
if err != nil {
panic(err)
}
// custom headers should in response data
index := utils.SliceIndex(len(res.Headers), func(num int) bool {
return res.Headers[num] == Header{"Foo", "bar"}
})
fmt.Printf("%+v", res.Headers[index])
}
Output: {Name:Foo Value:bar}
func PostJSON ¶
PostJSON make a http post request with custom body and headers
Example ¶
Example PostJSON make a simple http post request
package main
import (
"fmt"
"github.com/zcong1993/utils"
)
func main() {
// define a response data type
type User struct {
Username string `decoder:"username"`
Age int `decoder:"age"`
}
type Resp struct {
Status string `decoder:"status"`
User User `decoder:"user"`
}
// post body
user := User{"zcong", 18}
var res Resp
// this api return post body in response.user
err := utils.PostJSON("http://zcong-hello.getsandbox.com/users", &user, &res, map[string]string{})
// check error
if err != nil {
panic(err)
}
// res is response json data
fmt.Printf("%+v", res)
}
Output: {Status:ok User:{Username:zcong Age:18}}
func SliceIndex ¶
SliceIndex return the index of the first element in the array that satisfies the provided testing function.
Otherwise -1 is returned
Example ¶
Example SliceIndex returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned
package main
import (
"fmt"
"github.com/zcong1993/utils"
)
func main() {
arr := []int{1, 2, 3, 4}
// test function, find index of 3 in arr
condition := func(i int) bool {
return arr[i] == 3
}
index := utils.SliceIndex(len(arr), condition)
fmt.Println(index)
}
Output: 2
func StringAddress ¶
StringAddress is a helper function to get const string address
Types ¶
This section is empty.