在Golang中初始化具有不同大小的数组数组。

huangapple go评论120阅读模式
英文:

Initialize array of arrays with different sizes in Golang

问题

在Golang中,我想初始化一个数组的数组,用来表示一年的数据。
主数组将有12个子数组,每个子数组代表一个月。每个子数组的大小将根据它们代表的月份而定。

例如,第0个子数组将代表一月,并包含31个元素;第1个子数组将代表二月,并包含29个元素(考虑闰年),依此类推。

在Golang中,多维数组可以使用[12][30]int这样的方式进行初始化,但在这里,我需要使用不同大小的多维数组。我该如何初始化这样一种数据类型的变量呢?

简而言之,我想做的事情类似于这样:

  1. var year = [12][31]int{{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}

但是我在语法上遇到了困难。有人可以帮我解决吗?谢谢!

英文:

In Golang, I want to initialize an array of arrays which can represent an year.
The main array will have 12 sub-arrays each will represent a month. Each subarray will have size of the month they represent.

For eg, 0th sub-array will reference January and will contain 31 elements, 1st sub-array will reference February and will contain 29 elements (considering a leap year) and so on...

Multi-dimensional arrays are initialized like [12][30]int in Golang considering the fixed size. But here, I am working with a multidimensional array of different sizes. How do I initialize a variable with such a datatype?

In short, I want to do something like this:

  1. var year [[31]int, [29]int, [31]int, [30]int, [31]int, [30]int, [31]int, [31]int, [30]int, [31]int, [30]int, [31]int]

But I am struggling with the syntax. Could someone please help me out? Thanks!!

答案1

得分: 2

你想要这样的代码吗?

  1. package main
  2. import "fmt"
  3. func main() {
  4. days := []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  5. months := [12][]int{}
  6. for i := 0; i < 12; i++ {
  7. months[i] = make([]int, days[i])
  8. fmt.Println(i, months[i], len(months[i]))
  9. }
  10. }

在 Go Playground 上运行:链接

英文:

You want something like this?

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. days := []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  5. months := [12][]int{}
  6. for i := 0; i &lt; 12; i++ {
  7. months[i] = make([]int, days[i])
  8. fmt.Println(i, months[i], len(months[i]))
  9. }
  10. }

run in go playground

答案2

得分: 2

稍微更一致的答案参考@Sakib。我假设你也想填写日期。

  1. days := []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  2. months := make([][]int, len(days))
  3. for i := 0; i < 12; i++ {
  4. months[i] = make([]int, days[i])
  5. for j := 0; j < days[i]; j++ {
  6. months[i][j] = j + 1
  7. }
  8. fmt.Println(i+1, months[i])
  9. }
英文:

A slightly more consistent answer reference @Sakib. And I assumed you wanted the dates to be filled too.

  1. days := []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  2. months := make([][]int, len(days))
  3. for i := 0; i &lt; 12; i++ {
  4. months[i] = make([]int, days[i])
  5. for j := 0; j &lt; days[i]; j++ {
  6. months[i][j] = j + 1
  7. }
  8. fmt.Println(i+1, months[i])
  9. }

答案3

得分: 1

如果你想要初始化一个表示年份的二维数组,其中多维数组的初始化方式类似于 [12][30]int,我认为以下代码可以解决你的问题:

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "strconv"
  7. "time"
  8. )
  9. func main() {
  10. years := []int{
  11. 2022,
  12. 1972,
  13. 2021,
  14. 2020,
  15. }
  16. var s string
  17. for _, year := range years {
  18. s = ""
  19. ret, err := GetYearDays(year)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. for i := range ret {
  24. s = s + " " + strconv.Itoa(len(ret[i]))
  25. }
  26. log.Printf("year: %d, month days:%s\n", year, s)
  27. }
  28. }
  29. func GetYearDays(year int) (ret [12][]int, err error) {
  30. // year check depends on the user
  31. if year < 1952 || year > 3000 {
  32. return ret, errors.New("invalid year")
  33. }
  34. for idx := range ret {
  35. firstDay, err := time.Parse("2006-1-2", fmt.Sprintf("%d-%d-1", year, idx+1))
  36. if err != nil {
  37. return ret, fmt.Errorf("time invalid, year: %d, month: %d", year, idx+1)
  38. }
  39. lastDay := firstDay.AddDate(0, 1, -1)
  40. ret[idx] = make([]int, lastDay.Day())
  41. }
  42. return ret, err
  43. }

希望对你有帮助!

英文:

if you want to initialize an array of arrays which can represent an year and multi-dimensional arrays are initialized like [12][30]int, I think this code may solve your problem:

  1. package main
  2. import (
  3. &quot;errors&quot;
  4. &quot;fmt&quot;
  5. &quot;log&quot;
  6. &quot;strconv&quot;
  7. &quot;time&quot;
  8. )
  9. func main() {
  10. years := []int{
  11. 2022,
  12. 1972,
  13. 2021,
  14. 2020,
  15. }
  16. var s string
  17. for _, year := range years {
  18. s = &quot;&quot;
  19. ret, err := GetYearDays(year)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. for i := range ret {
  24. s = s + &quot; &quot; + strconv.Itoa(len(ret[i]))
  25. }
  26. log.Printf(&quot;year: %d, month days:%s\n&quot;, year, s)
  27. }
  28. }
  29. func GetYearDays(year int) (ret [12][]int, err error) {
  30. // year check depends on the user
  31. if year &lt; 1952 || year &gt; 3000 {
  32. return ret, errors.New(&quot;invalid year&quot;)
  33. }
  34. for idx := range ret {
  35. firstDay, err := time.Parse(&quot;2006-1-2&quot;, fmt.Sprintf(&quot;%d-%d-1&quot;, year, idx+1))
  36. if err != nil {
  37. return ret, fmt.Errorf(&quot;time invalid, year: %d, month: %d&quot;, year, idx+1)
  38. }
  39. lastDay := firstDay.AddDate(0, 1, -1)
  40. ret[idx] = make([]int, lastDay.Day())
  41. }
  42. return ret, err
  43. }

huangapple
  • 本文由 发表于 2022年8月14日 18:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/73350712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定