How to append double bracket string?

How to append double bracket string

var buttons = [][]string{}
var dates = time.Now()
var years = dates.Year()
for i := years; i <= (years - 5); i-- {
   var converti = strconv.Itoa(i)
   buttons = append(buttons, "A"+converti)
}

Hi, you made a couple of mistakes there. First, unless I’m getting your idea wrong, you need an array of strings, not an array of arrays of strings. Second, the for condition is backwards, as you are going down from i initialized as current year to (current year - 5), so you want to loop while i is equal or greater than (years - 5), not lower than (current year is 2018, so in your code on first iteration you’ll compare 2018 <= 2013, leaving the for loop immediately).

So, keeping almost everything the same and just fixing the errors, I think you are looking for something like this:

package main

import (
	"fmt"
	"time"
	"strconv"
)

func main() {
	var buttons = []string{}
	var dates = time.Now()
	var years = dates.Year()
	for i := years; i >= (years - 5); i-- {
   		var converti = strconv.Itoa(i)
   		buttons = append(buttons, "A"+converti)
	}
	fmt.Println(buttons)
}

I added the Println at the end, which in the playground prints this (date is fixed to 2009-11-10 23:00:00 there, so the initial year is 2009 instead of 2018):

[A2009 A2008 A2007 A2006 A2005 A2004]

Is that your expected result?

1 Like

Yes that is my expected result, but default array is using double bracket string
please check keyboardhandler function in this url https://github.com/yanzay/tbot

image

{“Some”, “Test”, “Buttons”} and {“Another”,“Row”} are not strings, they are []string arrays. Thus, buttons, which is [][]string (an array of string arrays) may be intialized with those two values.

Take a look at this example to see how it works (you can run it here):

package main

import (
	"fmt"
)

func main() {

	buttons := [][]string {
		{"Some", "Test", "Buttons"},
		{"Another","Row"},
	}
	
	fmt.Printf("The whole array: %v\n", buttons)
	fmt.Printf("First member of the array: %v\n", buttons[0])
	fmt.Printf("Second member of the array: %v\n", buttons[1])
	
	fmt.Printf("First member of the first array from buttons: %v\n", buttons[0][0])
	fmt.Printf("First member of the second array from buttons: %v\n", buttons[1][0])
}

Also, you seem to be new to Go. I suggest you take the Go Tour to get to know the language: https://tour.golang.org/welcome/1

1 Like

Thanks @iegomez so the question is, how to create array of string arrays using dynamics data? such as looping or database?

It kind of depends on what’s of a dynamic nature: the “big” array, the strings arrays, everything?

I’ll just give you an exaple of a known sized for the container array which gets filled with string arrays on a for loop, with one of them having string dynamically appended to it:

package main

import (
	"fmt"
)

func main() {
	n := 10
	
	//The container array has fixed size.
	var bigArray = make([][]string, n, n)
	for i := 0; i < n; i ++ {
	
		//We don't know size of string arrays
  		bigArray[i] = []string{}
		
		
		//Let's add some strings for the array at position 5 by appending to bigArray[5]
		if i == 5 {
			for j := 0; j < 5; j++ {
				bigArray[i] = append(bigArray[i], fmt.Sprintf("dynamic string %d", j))
			}
		}
		
		fmt.Printf("strings array at %d has len %d\n", i, len(bigArray[i]))
	}
	
	//Now, let's see what's in bigArray[5].
	for i, str := range bigArray[5] {
		fmt.Printf("found %s at postion %d of bigArray[5]\n", str, i)
	}
}

And here’s on the playground: https://play.golang.org/p/fagiJyNW_UU

But again, I suggest you take the Go Tour and read everything you can about Go, write some dummy programs, etc., to practice the language.

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