123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- // Copyright 2013 com authors
- //
- // Licensed under the Apache License, Version 2.0 (the "License"): you may
- // not use this file except in compliance with the License. You may obtain
- // a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- // License for the specific language governing permissions and limitations
- // under the License.
- package com_test
- import (
- "fmt"
- "io/ioutil"
- "net/http"
- "notabug.org/makenotabuggreatagain/com"
- )
- // ------------------------------
- // cmd.go
- // ------------------------------
- func ExampleColorLogS() {
- coloredLog := com.ColorLogS(fmt.Sprintf(
- "[WARN] This is a tesing log that should be colored, path( %s ),"+
- " highlight # %s #, error [ %s ].",
- "path to somewhere", "highlighted content", "tesing error"))
- fmt.Println(coloredLog)
- }
- func ExampleColorLog() {
- com.ColorLog(fmt.Sprintf(
- "[WARN] This is a tesing log that should be colored, path( %s ),"+
- " highlight # %s #, error [ %s ].",
- "path to somewhere", "highlighted content", "tesing error"))
- }
- func ExampleExecCmd() {
- stdout, stderr, err := com.ExecCmd("go", "help", "get")
- fmt.Println(stdout, stderr, err)
- }
- // ------------- END ------------
- // ------------------------------
- // html.go
- // ------------------------------
- func ExampleHtml2JS() {
- htm := "<div id=\"button\" class=\"btn\">Click me</div>\n\r"
- js := string(com.Html2JS([]byte(htm)))
- fmt.Println(js)
- // Output: <div id=\"button\" class=\"btn\">Click me</div>\n
- }
- // ------------- END ------------
- // ------------------------------
- // path.go
- // ------------------------------
- func ExampleGetGOPATHs() {
- gps := com.GetGOPATHs()
- fmt.Println(gps)
- }
- func ExampleGetSrcPath() {
- srcPath, err := com.GetSrcPath("notabug.org/makenotabuggreatagain/com")
- if err != nil {
- fmt.Println(err)
- return
- }
- fmt.Println(srcPath)
- }
- func ExampleHomeDir() {
- hd, err := com.HomeDir()
- fmt.Println(hd, err)
- }
- // ------------- END ------------
- // ------------------------------
- // file.go
- // ------------------------------
- func ExampleIsFile() {
- if com.IsFile("file.go") {
- fmt.Println("file.go exists")
- return
- }
- fmt.Println("file.go is not a file or does not exist")
- }
- func ExampleIsExist() {
- if com.IsExist("file.go") {
- fmt.Println("file.go exists")
- return
- }
- fmt.Println("file.go does not exist")
- }
- // ------------- END ------------
- // ------------------------------
- // dir.go
- // ------------------------------
- func ExampleIsDir() {
- if com.IsDir("files") {
- fmt.Println("directory 'files' exists")
- return
- }
- fmt.Println("'files' is not a directory or does not exist")
- }
- // ------------- END ------------
- // ------------------------------
- // string.go
- // ------------------------------
- func ExampleIsLetter() {
- fmt.Println(com.IsLetter('1'))
- fmt.Println(com.IsLetter('['))
- fmt.Println(com.IsLetter('a'))
- fmt.Println(com.IsLetter('Z'))
- // Output:
- // false
- // false
- // true
- // true
- }
- //func ExampleExpand() {
- // match := map[string]string{
- // "domain": "gowalker.org",
- // "subdomain": "github.com",
- // }
- // s := "http://{domain}/{subdomain}/{0}/{1}"
- // fmt.Println(com.Expand(s, match, "Unknwon", "gowalker"))
- // Output: http://gowalker.org/notabug.org/makenotabuggreatagain/gowalker
- //}
- // ------------- END ------------
- // ------------------------------
- // http.go
- // ------------------------------
- func ExampleHttpGet() ([]byte, error) {
- rc, err := com.HttpGet(&http.Client{}, "http://gowalker.org", nil)
- if err != nil {
- return nil, err
- }
- p, err := ioutil.ReadAll(rc)
- rc.Close()
- return p, err
- }
- func ExampleHttpGetBytes() ([]byte, error) {
- p, err := com.HttpGetBytes(&http.Client{}, "http://gowalker.org", nil)
- return p, err
- }
- func ExampleHttpGetJSON() interface{} {
- j := com.HttpGetJSON(&http.Client{}, "http://gowalker.org", nil)
- return j
- }
- type rawFile struct {
- name string
- rawURL string
- data []byte
- }
- func (rf *rawFile) Name() string {
- return rf.name
- }
- func (rf *rawFile) RawUrl() string {
- return rf.rawURL
- }
- func (rf *rawFile) Data() []byte {
- return rf.data
- }
- func (rf *rawFile) SetData(p []byte) {
- rf.data = p
- }
- func ExampleFetchFiles() {
- // Code that should be outside of your function body.
- // type rawFile struct {
- // name string
- // rawURL string
- // data []byte
- // }
- // func (rf *rawFile) Name() string {
- // return rf.name
- // }
- // func (rf *rawFile) RawUrl() string {
- // return rf.rawURL
- // }
- // func (rf *rawFile) Data() []byte {
- // return rf.data
- // }
- // func (rf *rawFile) SetData(p []byte) {
- // rf.data = p
- // }
- files := []com.RawFile{
- &rawFile{rawURL: "http://example.com"},
- &rawFile{rawURL: "http://example.com/foo"},
- }
- err := com.FetchFiles(&http.Client{}, files, nil)
- fmt.Println(err, len(files[0].Data()), len(files[1].Data()))
- }
- func ExampleFetchFilesCurl() {
- // Code that should be outside of your function body.
- // type rawFile struct {
- // name string
- // rawURL string
- // data []byte
- // }
- // func (rf *rawFile) Name() string {
- // return rf.name
- // }
- // func (rf *rawFile) RawUrl() string {
- // return rf.rawURL
- // }
- // func (rf *rawFile) Data() []byte {
- // return rf.data
- // }
- // func (rf *rawFile) SetData(p []byte) {
- // rf.data = p
- // }
- files := []com.RawFile{
- &rawFile{rawURL: "http://example.com"},
- &rawFile{rawURL: "http://example.com/foo"},
- }
- err := com.FetchFilesCurl(files)
- fmt.Println(err, len(files[0].Data()), len(files[1].Data()))
- }
- // ------------- END ------------
- // ------------------------------
- // regex.go
- // ------------------------------
- func ExampleIsEmail() {
- fmt.Println(com.IsEmail("test@example.com"))
- fmt.Println(com.IsEmail("@example.com"))
- // Output:
- // true
- // false
- }
- func ExampleIsUrl() {
- fmt.Println(com.IsUrl("http://example.com"))
- fmt.Println(com.IsUrl("http//example.com"))
- // Output:
- // true
- // false
- }
- // ------------- END ------------
- // ------------------------------
- // slice.go
- // ------------------------------
- func ExampleAppendStr() {
- s := []string{"a"}
- s = com.AppendStr(s, "a")
- s = com.AppendStr(s, "b")
- fmt.Println(s)
- // Output: [a b]
- }
- // ------------- END ------------
|