普通网友 2018-03-29 09:06
浏览 6

我的失误是什么?

package main
import "fmt"
func main(){
        a:=[]int{1,2,3,4,5}           //slice of int
        b:=[]struct{                  //another slice of struct
                i int
                j string
        }{
                {1,"精"},
                {2 ,"コバや歌詞"},
                {3,"新一"},
                {4,"武士"},
        }
        c:=[]struct{                           //slice of slices
                d []struct
                e []int
        }{
                {a[:],b[:]},
        }


        fmt.Println(a,b,c)
}

The errors are:-

  • ./slices.go:16:3: syntax error: unexpected e, expecting {
  • ./slices.go:17:3: syntax error: unexpected {, expecting semicolon or newline or }
  • ./slices.go:18:7: syntax error: unexpected ] at end of statement
  • 写回答

2条回答 默认 最新

  • dongyi7513 2018-03-29 09:09
    关注

    You are creating c struct wrong. It should contain slice of int for first field and slice of struct of b type as second field.

        c:=[]struct{                           //slice of slices
                d []int
                e []struct{               //another slice of struct
                    i int
                    j string
                }
        }{
                {a[:],b[:]},
        }
    

    Below code is working

    package main
    import "fmt"
    func main(){
         a:=[]int{1,2,3,4,5}           //slice of int
         b:=[]struct{                  //another slice of struct
                    i int
                    j string
         }{
                 {1,"精"},
                 {2 ,"コバや歌詞"},
                 {3,"新一"},
                 {4,"武士"},
         }
         c:=[]struct{                           //slice of slices
                d []int
                e []struct{               //another slice of struct
                    i int
                    j string
                }
         }{
                {a[:],b[:]},
         }
         fmt.Println(a,b,c)
    }
    

    Go Playground

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?