dongsu3654 2018-02-14 19:08
浏览 38

类型声明的位置

I am curious if this type of declaration for structs

 t := Person{"girlie", 12} 

only works if its type is declared in the same file.
Below are my files.

file st.go, type def structure inside to be used in main func

 package structs

 type person struct {
     age int
     name int
 }

file practice.go, main function:

 package main

 import( 
       "fmt"
       "structs/dir"
   )    

   func main() {

        var s dir.Person
        s.Name = "She"
        s.Age = 12

 >>     t := Person{"girlie", 12}



        fmt.Println(s.Name)
        fmt.Println(t.Name)
   } 

As you can see an error occurs where instance t is declared.

  • 写回答

1条回答 默认 最新

  • dta25920 2018-02-14 19:13
    关注

    You're missing the package reference, it should be:

    t := dir.Person{"girlie", 12}
    //   ^^^^ missing this part
    

    Assuming that the line var s dir.Person works, which I'm guessing it does based on the question, which means that your quoted contents of "st.go" are not accurate, because the package name is different, and the struct and its fields are not exported.

    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢