前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

golang

作者头像
hotqin888
发布2018-09-11 11:51:05
7271
发布2018-09-11 11:51:05
举报
文章被收录于专栏:hotqin888的专栏hotqin888的专栏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1333956

代码语言:javascript
复制
package main

import (
	"fmt"
	//	"path"
	//	"runtime"

	//	"os"
	//	"regexp"
	//	"strings"
	//	"time"
	"encoding/json"
	//	"sort"
)

func main() {
	Example()
}

type List struct {
	Url    string
	Source string
	State  string
}

type Person struct {
	Name        string
	Age         int
	Slices      []string //slice
	Mapstring   map[string]string
	StructArray []List            //结构体的切片型
	MapStruct   map[string][]List //map:key类型是string或struct,value类型是切片,切片的类型是string或struct
	//	Desks  List
}

//func (p Person) String() string {
//	return fmt.Sprintf("%s: %d", p.Name, p.Age)
//}

// ByAge implements sort.Interface for []Person based on
// the Age field.
//type ByAge []Person

//func (a ByAge) Len() int           { return len(a) }
//func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
//func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func Example() {
	//map 序列化为json
	var m map[string]string = make(map[string]string)
	m["Go"] = "No.1"
	m["Java"] = "No.2"
	m["C"] = "No.3"
	fmt.Println(m)

	list := []List{
		{"upload/1.jpg", "http://a.com/1.jpg", "SUCCESS"},
		{"upload/2.jpg", "http://b.com/2.jpg", "SUCCESS"},
	}

	var mm map[string][]List = make(map[string][]List)
	mm["Go"] = list
	mm["Java"] = list
	//	m["C"] = "No.3"
	fmt.Println(mm)

	people := []Person{
		{"Bob", 31, []string{"hello", "apple"}, m, list, mm},
		//		{"John", 42, []string{"python", "golang"}, m},
		//		{"Michael", 22, []string{"base", "peach", "pear"},map[string]{"a":"haha"}},
		//		{"Jenny", 26, []string{"python", "golang", "pear"},map[string]{"a":"haha"}},
	}
	fmt.Println(people)
	//	sort.Sort(ByAge(people))
	//	fmt.Println(people)
	b, _ := json.Marshal(people)
	fmt.Println(string(b))
	//	if bs, err := json.Marshal(m); err != nil {
	//		panic(err)
	//	} else {
	//		//result --> {"C":"No.3","Go":"No.1","Java":"No.2"}
	//		fmt.Println(string(bs))
	//	}
	// Output:
	// [Bob: 31 John: 42 Michael: 17 Jenny: 26]
	// [Michael: 17 Jenny: 26 Bob: 31 John: 42]
}

//type person struct {
//    name string
//    age int
//}
//var P person  // P现在就是person类型的变量了
//P.name = "Astaxie"  // 赋值"Astaxie"给P的name属性.
//P.age = 25  // 赋值"25"给变量P的age属性
//fmt.Printf("The person's name is %s", P.name)  // 访问P的name属性.
//除了上面这种P的声明使用之外,还有另外几种声明使用方式:
//1.按照顺序提供初始化值
//复制代码 代码如下:

//P := person{"Tom", 25}

//2.通过field:value的方式初始化,这样可以任意顺序
//复制代码 代码如下:

//P := person{age:24, name:"Tom"}

//3.当然也可以通过new函数分配一个指针,此处P的类型为*person
//复制代码 代码如下:

//P := new(person)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年01月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档