dongzhijing8202 2018-03-20 19:57
浏览 36
已采纳

包以返回结构图

I have created the logic to populate a map of structs and it is working as expected. Now, I would like to move that functionality to a package. Like so

package returnperson
func Person() map[string]personstruct {
   //do stuff
   return people
}

I defined the personstruct in the body of the function Person, however, line 2 is throwing the following error:

undefined:personstruct(2,1)

I've researched and I don't seem to find a way to correct the issue. Thanks in advance for your help.

  • 写回答

1条回答 默认 最新

  • duanfu1945 2018-03-20 20:05
    关注

    Here a few hints on extracting something to a package:

    1. Only functions, variables and types with capital letters on package level are accessible from the "outside" (other packages or main)

    2. A public function should only return types that are public as well

    In your case you say personstruct is defined inside the function Person. That is not possible if you want it to be a return value of the function. You need to define it on package level. Also it should be exported since an exported function uses it.

    Here a sample:

    package returnperson
    
    // PersonStruct defines ...
    type PersonStruct struct {
        // fields
    }
    
    // Person does ...
    func Person() map[string]PersonStruct {
        var people = map[string]PersonStruct{}
        //do stuff
        return people
    }
    

    -- edit --

    I would further suggest to call the package itself person and make it about managing a person, meaning the main item in this package would be a struct called Person. returnperson is more of a function name.

    The package could also be about a list of persons (slice/map). Then maybe you want to call it personlist and the main topic of the package would be managing a list of persons with the main struct maybe called PersonList.

    Just some suggestions to think about...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题