Golang中返回结构体或数组的函数实现方法

Golang中返回结构体或数组的函数实现方法 大家好,我是Go语言函数编写的新手。对于如何编写可复用的代码还不太熟悉。我知道如何返回单个变量,但在返回数组或结构体方面甚至没有尝试过。以下是示例代码:

type a struct {
	Title   []string
	Article [][]string
}
func test1(res http.ResponseWriter, req *http.Request) {
	var data = &a{
		Title: []string{"One", "Two", "Three"},
		Article: [][]string{
			[]string{"a", "b", "c","b"},
			[]string{},
			[]string{"f", "g", "h", "i"}},
	}
	templating.Display(res, "test1", data)
}	
func test2(res http.ResponseWriter, req *http.Request) {
	var data = &a{
		Title: []string{"One", "Two", "Three"},
		Article: [][]string{
			[]string{"a", "b", "c","b"},
			[]string{},
			[]string{"f", "g", "h", "i"}},
	}
	templating.Display(res, "test1", data)
}	

我知道数据变量应该负责处理记录,但这样写看起来不太美观。我认为更好的做法是将其放在一个函数中,然后调用该函数,但我不知道该如何实现这样的结构。


更多关于Golang中返回结构体或数组的函数实现方法的实战教程也可以访问 https://www.itying.com/category-94-b0.html

7 回复

谢谢

更多关于Golang中返回结构体或数组的函数实现方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


我仍然不明白。最后的代码片段构造了一个 a 实例。您想用函数做什么?

必须声明返回类型:

func data() a {
//
}

n_oandasan:

我认为最好放在一个函数中然后调用该函数

你这是什么意思?我不太清楚你的问题是什么。

我想以函数形式调用

var data = &a{
Title: []string{"One", "Two", "Three"},
Article: [][]string{
[]string{"a", "b", "c","b"},
[]string{},
[]string{"f", "g", "h", "i"}},
}

我可以这样使用吗?我调用的函数?

type a struct {
    Title   []string
    Article [][]string
}
function **data**(){
    data = &a{
        Title: []string{"One", "Two", "Three"},
        Article: [][]string{
            []string{"a", "b", "c","b"},
            []string{},
            []string{"f", "g", "h", "i"}},
    }
}
func test1(res http.ResponseWriter, req *http.Request) {
    
    templating.Display(res, "test1", **data**)
}	
func test2(res http.ResponseWriter, req *http.Request) {
    
    templating.Display(res, "test1", **data**)
}

在Go语言中,返回结构体或数组的函数实现非常直接。基于你的代码,可以创建一个返回结构体指针的函数来避免重复代码。以下是具体实现:

type a struct {
    Title   []string
    Article [][]string
}

// 返回结构体指针的函数
func getData() *a {
    return &a{
        Title: []string{"One", "Two", "Three"},
        Article: [][]string{
            {"a", "b", "c", "b"},
            {},
            {"f", "g", "h", "i"},
        },
    }
}

func test1(res http.ResponseWriter, req *http.Request) {
    data := getData()
    templating.Display(res, "test1", data)
}

func test2(res http.ResponseWriter, req *http.Request) {
    data := getData()
    templating.Display(res, "test1", data)
}

如果需要返回数组,可以这样实现:

// 返回字符串数组的函数
func getTitles() []string {
    return []string{"One", "Two", "Three"}
}

// 返回二维字符串数组的函数
func getArticles() [][]string {
    return [][]string{
        {"a", "b", "c", "b"},
        {},
        {"f", "g", "h", "i"},
    }
}

// 使用数组返回值的函数
func test3(res http.ResponseWriter, req *http.Request) {
    data := &a{
        Title:   getTitles(),
        Article: getArticles(),
    }
    templating.Display(res, "test1", data)
}

对于更灵活的解决方案,可以创建带参数的函数:

func createData(titles []string, articles [][]string) *a {
    return &a{
        Title:   titles,
        Article: articles,
    }
}

func test4(res http.ResponseWriter, req *http.Request) {
    titles := []string{"Four", "Five", "Six"}
    articles := [][]string{
        {"x", "y", "z"},
        {"1", "2", "3"},
    }
    data := createData(titles, articles)
    templating.Display(res, "test1", data)
}

这些实现方式提供了代码复用性,同时保持了类型安全和清晰的代码结构。

回到顶部