go: add elements to array

Here is one way, how to add elements to array in go language, see https://play.golang.org/p/nCLX23ymRUM

package main

import (

type MyRecord struct {
	Name  string
	Phone string

var recA = MyRecord{Name: "Anna", Phone: "123"}
var recB = MyRecord{Name: "Bob", Phone: "456"}

type Records []MyRecord

func buildAppendedArrayPtr() {
	arr := make(Records, 0)
	arrPtr := &arr
	arrPtr = appendToArrayPtr(arrPtr, recA)
	arrPtr = appendToArrayPtr(arrPtr, recB)

	fmt.Printf("%v %T %d \n", arrPtr, arrPtr, len(*arrPtr))

	for i, rec := range *arrPtr {
		fmt.Printf("%d. %v\n", i, rec)

func appendToArrayPtr(recordsPtr *Records, myRecord MyRecord) *Records {
	arr := append(*recordsPtr, myRecord)
	return &arr

func main() {

This entry was posted in golang, workday. Bookmark the permalink.

Leave a Reply