golang 入门之struct继承,嵌套
package main
import "fmt"
type Jocongmin struct{
Name string
Home string
Want string
}
func (j *Jocongmin) SayName() string{ //这里定义的方法是拓展方法,是对Jocongmin这个struct的继承,也就是struct多了一个Say..方法,可以通过Jocongmin这个struct实例来调用
return "我的名字是"+j.Name
}
func (j *Jocongmin) SayHome() string{
return "我的家在"+j.Home
}
func (j *Jocongmin) SayWant() string{
return "我的喜爱是"+j.Want
}
func main(){
var jocongmin Jocongmin
jocongmin.Name="周聪明"
jocongmin.Home="福建省"
jocongmin.Want="money"
fmt.Println(jocongmin.SayName()) //调用了struct的方法
fmt.Println(jocongmin.SayHome())
fmt.Println(jocongmin.SayWant())
}
2.struct的嵌套
package main import "encoding/json"
import "fmt"
import "strconv"
import (
"github.com/drone/routes"
"net/http"
) type ResInfo struct { //定义一个struct,然后这个struct里面有哪些子对象
Data YearDataStruct
Msg string
} type YearDataStruct struct {
MouthAll []MouthStruct //定义一个类型为数组的对象,然后这个数组的元素类型为某种struct
Sum DetailStruct
Average DetailStruct
Quarter []QuarterStruct
}
type DetailStruct struct {
One int
Two int
Three int
}
type QuarterStruct struct {
DetailStruct //可以嵌套复合其他类型的struct,这样就继承下了其他struct的子对象
QuarterNum int
} type MouthStruct struct {
Mouth int
PartmentItem []ItemArrStruct
}
type ItemArrStruct struct {
PartMent string
DetailStruct
} func cross(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域
w.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header的类型
w.Header().Set("content-type", "application/json") //返回数据格式是json
}
func main() {
fmt.Println("正在启动WEB服务...")
var mux *routes.RouteMux = routes.New()
mux.Get("/test", DataSend)
//http.Handle("/", mux)
http.ListenAndServe(":8088", mux)
fmt.Println("服务已停止")
} func DataSend(w http.ResponseWriter, r *http.Request) {
var data YearDataStruct
for i := 1; i < 13; i++ {
var partMent []ItemArrStruct
for t := 1; t < 4; t++ {
partMent = append(partMent, ItemArrStruct{
PartMent: "" + strconv.Itoa(t) + "abc",
DetailStruct: DetailStruct{ //继承其他struct的子对象的赋值方法是这样的,one two 之类的和partment是同一等级
One: 45,
Two: 23,
Three: 3432,
},
})
}
data.MouthAll = append(data.MouthAll, MouthStruct{
Mouth: i,
PartmentItem: partMent,
})
}
for i := 1; i <= 4; i++ {
data.Quarter = append(data.Quarter, QuarterStruct{
DetailStruct: DetailStruct{
One: 4324,
Two: 23423,
Three: 4324234,
},
QuarterNum: i,
})
}
data.Sum = DetailStruct{
One: 4546,
Two: 454,
Three: 454,
}
data.Average = DetailStruct{
One: 4546,
Two: 454,
Three: 465465,
}
var res ResInfo
res.Data = data
res.Msg = "right"
resJson, _ := json.Marshal(res)
fmt.Fprintln(w, string(resJson))
}
golang 入门之struct继承,嵌套的更多相关文章
- Golang 入门 : 结构体(struct)
Go 通过类型别名(alias types)和结构体的形式支持用户自定义类型,或者叫定制类型.试图表示一个现实世界中的实体. 结构体由一系列命名的元素组成,这些元素又被称为字段,每个字段都有一个名称和 ...
- golang中结构体的嵌套、方法的继承、方法的重写
package main import "fmt" type human struct { name, phone string age int8 } type student s ...
- Java程序员的Golang入门指南(上)
Java程序员的Golang入门指南 1.序言 Golang作为一门出身名门望族的编程语言新星,像豆瓣的Redis平台Codis.类Evernote的云笔记leanote等. 1.1 为什么要学习 如 ...
- Java程序员的Golang入门指南(下)
Java程序员的Golang入门指南(下) 4.高级特性 上面介绍的只是Golang的基本语法和特性,尽管像控制语句的条件不用圆括号.函数多返回值.switch-case默认break.函数闭包.集合 ...
- Go基础系列:struct和嵌套struct
struct struct定义结构,结构由字段(field)组成,每个field都有所属数据类型,在一个struct中,每个字段名都必须唯一. 说白了就是拿来存储数据的,只不过可自定义化的程度很高,用 ...
- Golang面向对象编程-struct(结构体)
Golang面向对象编程-struct(结构体) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是面向对象编程 面向对象编程(Object Oriented Program ...
- Golang入门(3):一天学完GO的进阶语法
摘要 在上一篇文章中,我们聊了聊Golang中的一些基础的语法,如变量的定义.条件语句.循环语句等等.他们和其他语言很相似,我们只需要看一看它们之间的区别,就差不多可以掌握了,所以作者称它们为&quo ...
- Golang 入门 : 竞争条件
笔者在前文<Golang 入门 : 理解并发与并行>和<Golang 入门 : goroutine(协程)>中介绍了 Golang 对并发的原生支持以及 goroutine 的 ...
- Golang 入门 : goroutine(协程)
在操作系统中,执行体是个抽象的概念.与之对应的实体有进程.线程以及协程(coroutine).协程也叫轻量级的线程,与传统的进程和线程相比,协程的最大特点是 "轻"!可以轻松创建上 ...
随机推荐
- 原生JavaScript实现跨域
为什么需要跨域呢?这是因为我们一般的请求都是使用xhr的,但是它只能调用同一个域里面的接口,有时候,我们想要在自己的站点中调用其他站点的接口,这时候就要用到跨域了.其实,跨域并不难,我们可以通过Jav ...
- Reactor/Proactor的比较 (ZZ)
一般情况下,I/O 复用机制需要事件分享器(event demultiplexor [1.3]). 事件分享器的作用,即将那些读写事件源分发给各读写事件的处理者,就像送快递的在楼下喊: 谁的什么东西送 ...
- 【转】ImageView.ScaleType属性
原文网址:https://blog.csdn.net/Buaaroid/article/details/49360779 ImageView的Scaletype决定了图片在View上显示时的样子,如进 ...
- MySQL 建表语句 create table 中的列定义
MySQL 建表语句 create table 中的列定义: column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value ...
- Hanlp在java中文分词中的使用介绍
项目结构 该项目中,.jar和data文件夹和.properties需要从官网/github下载,data文件夹下载 项目配置 修改hanlp.properties: 1 #/Test/src/han ...
- Linux mktemp命令
mktemp命令 Linux mktemp命令用于建立暂存文件.mktemp建立的一个暂存文件,供shell script使用.主要特点就是可以做到每次执行mktemp时产生文件和目录都不重名:这个特 ...
- windbg 如何再内核模式调试用户空间的程序
1:使用!process 0 0 获取用户空间的所有的进程的信息 !process 0 0 **** NT ACTIVE PROCESS DUMP **** PROCESS 80a02a60 ...
- DEVC++ C++ Builder6.0
Devc++安装后无法正常编译程序 出现错误,不知道是什么,可能是不兼容的原因 然后就是一直编译出错,程序是最简单的helloworld程序. 之后选择安装C++ Builder 6.0
- Excel技巧--使用规划求解
当我们需要求在有限预算下可以购买的商品数量时,我们就可以使用“规划求解”功能.如上图,在1000元的预算目标内,我们能购买左图中的各书籍多少本.而这些数量,就可以使用“规划求解”来获取答案. 1.实际 ...
- Flask--异常处理
异常处理: abort(404)-捕获HTTP抛出的统一状态码 @app.errorhandler-捕获全局异常错误码,捕获异常错误 @app.route("/demo4") de ...