function that returns a 2d array of int and string – Go


So I’m trying to create a function that returns a 2D array that stores and integer value and a string value. Obviously i can just use a normal array like i would in python as go uses data types.

I’ve looked into using a struct but i don’t know if I’m missing something or not but I’ve deleted my small bit of code about 5 times and got nowhere, is anyone able to point me in the right direction please, i feel like I’m getting nowhere…

type user_info struct{
id int
name string

new_user := []user_info{{id: 1, name: "Max"}}

This will create one instance but how to i create a single array that i can append the new user to?


you have multiple ways to appned:

new_user := []user_info{
    {id: 1, name: "Max"},
    {id: 2, name: "John"},
    {id: 3, name: "George"},

or you can append item by item:

new_user = append(new_user, user_info{id: 4, name: "Sam"})

Answered By – Ronen

Answer Checked By – Cary Denson (GoLangFix Admin)

Leave a Reply

Your email address will not be published.