example_test.go 2.0 KB
Newer Older
martianzhang's avatar
martianzhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright 2018 Xiaomi, Inc.
 *
 * 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 common

import "fmt"

func ExampleFormatDSN() {
martianzhang's avatar
martianzhang 已提交
22
	Log.Debug("Entering function: %s", GetFunctionName())
martianzhang's avatar
martianzhang 已提交
23 24 25 26 27 28 29
	dsxExp := newDSN(nil)
	dsxExp.Addr = "127.0.0.1:3306"
	dsxExp.Schema = "mysql"
	dsxExp.User = "root"
	dsxExp.Password = "1t'sB1g3rt"
	dsxExp.Charset = "utf8mb4"
	dsxExp.Disable = false
martianzhang's avatar
martianzhang 已提交
30 31 32 33

	// 根据 &dsn 生成 dsnStr
	fmt.Println(FormatDSN(dsxExp))

martianzhang's avatar
martianzhang 已提交
34
	// Output: root:1t'sB1g3rt@tcp(127.0.0.1:3306)/mysql?charset=utf8mb4
martianzhang's avatar
martianzhang 已提交
35
	Log.Debug("Exiting function: %s", GetFunctionName())
martianzhang's avatar
martianzhang 已提交
36 37 38
}

func ExampleIsColsPart() {
martianzhang's avatar
martianzhang 已提交
39
	Log.Debug("Entering function: %s", GetFunctionName())
martianzhang's avatar
martianzhang 已提交
40 41 42 43 44
	// IsColsPart() 会 按照顺序 检查两个Column队列是否是包含(或相等)关系。
	a := []*Column{{Name: "1"}, {Name: "2"}, {Name: "3"}}
	b := []*Column{{Name: "1"}, {Name: "2"}}
	c := []*Column{{Name: "1"}, {Name: "3"}}
	d := []*Column{{Name: "1"}, {Name: "2"}, {Name: "3"}, {Name: "4"}}
45 46
	id := []*Column{{Name: "id"}}
	iD := []*Column{{Name: "iD"}}
martianzhang's avatar
martianzhang 已提交
47 48 49 50

	ab := IsColsPart(a, b)
	ac := IsColsPart(a, c)
	ad := IsColsPart(a, d)
51
	idiD := IsColsPart(id, iD) // 大小写对比
martianzhang's avatar
martianzhang 已提交
52

53 54
	fmt.Println(ab, ac, ad, idiD)
	// Output: true false true true
martianzhang's avatar
martianzhang 已提交
55
	Log.Debug("Exiting function: %s", GetFunctionName())
martianzhang's avatar
martianzhang 已提交
56 57 58
}

func ExampleSortedKey() {
martianzhang's avatar
martianzhang 已提交
59
	Log.Debug("Entering function: %s", GetFunctionName())
martianzhang's avatar
martianzhang 已提交
60 61 62 63 64 65 66 67 68 69
	ages := map[string]int{
		"a": 1,
		"c": 3,
		"d": 4,
		"b": 2,
	}
	for _, name := range SortedKey(ages) {
		fmt.Print(ages[name])
	}
	// Output: 1234
martianzhang's avatar
martianzhang 已提交
70
	Log.Debug("Exiting function: %s", GetFunctionName())
martianzhang's avatar
martianzhang 已提交
71
}