joken-前端工程师

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::
  394 随笔 :: 39 文章 :: 8 评论 :: 20万 阅读
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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的嵌套

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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))
}

  

posted on   joken1310  阅读(6783)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示