utils

package module
v0.0.0-...-22f4f1e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2018 License: MIT Imports: 7 Imported by: 5

README

utils

Build Status Go Report Card GoDoc

Some helper functions for go

Functions

SliceIndex

return the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned

GetJson

GetJson make a get request use http.Get

// 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: {200 hello world}
GetJsonWithHeaders

GetJsonWithHeaders make a http get request with custom headers

// define your response data type
type Resp struct {}
// define some custom headers
customHeaders := map[string]string{"Foo": "bar"}
var res Resp
// this api return all the request headers
err := utils.GetJSONWithHeaders("http://zcong-hello.getsandbox.com/header", &res, customHeaders)
// res is response data
Compile

Compile is a html template compiler with custom tpl and data

tpl := "hello {{.data}}"
data := map[string]string{"data": "world"}
var d bytes.Buffer
err := utils.Compile(&d, tpl, &data)
// check error
if err != nil {
    panic(err)
}
fmt.Printf("%s", d.String())
// hello world
CompileText

CompileText is same as Compile but use text/template

tpl := "hello {{.data}}"
data := map[string]string{"data": "world"}
var d bytes.Buffer
err := utils.CompileText(&d, tpl, &data)
// check error
if err != nil {
    panic(err)
}
fmt.Printf("%s", d.String())
// hello world

### StringAddress
Get address of const string
```go
str := "zcong1993"
add := StringAddress(str)
// now add is &str without error

License

MIT © zcong1993

Documentation

Overview

Package utils is a packages of some helper functions

See README.md for more info https://github.com/zcong1993/utils.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compile

func Compile(w io.Writer, tpl string, data interface{}) error

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

func CompileText(w io.Writer, tpl string, data interface{}) error

CompileText is same as Compile but use text/template

func GetJSON

func GetJSON(url string, v interface{}) error

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

func GetJSONWithHeaders(url string, v interface{}, headers map[string]string) error

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

func PostJSON(url string, body interface{}, v interface{}, headers map[string]string) error

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

func SliceIndex(limit int, condition func(num int) bool) int

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

func StringAddress(v string) *string

StringAddress is a helper function to get const string address

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL