Go function returning a struct or array

Guys, I’m new in creating function in go. I’m not familiar how to can i make it a reusable code. I know how to return single variable but in terms of array or struct im not even try. here is the sample code.

			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)
		}	

I know that the data variable should taking care of the record but it is ugly to see, i think is better to put in one function then call that function , but i don’t have any idea how can i arrive like that

What do you mean by that? It is not quite clear what your question is.

I want to call as a function

I still don’t understand. This last code snippet constructs a a instance. For what do you want to use a function?

Can i used this way? the function im calling?

    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**)
			}

You have to write the returned type :

func data() a {
//
}
1 Like

thanks

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.