duanqie8549 2013-12-31 00:55
浏览 118
已采纳

公共,私人-大写,小写:

New to GoLang, coming from Delphi, C++ :

First time I tried to create my own package in Go, I followed all the instructions about how to lay out the workspace, etc, but I kept on getting a compiler error:

./myPackage.go:52: undefined: myFunc

After poking around a bit I discovered that the public access modifier in Go is achieved simply by declaring a function in upper case. Great.

But when I started experimenting with the container classes - List for starters, I discovered I had to declare a List reference return value like this:

func GetFactors(value *int64) *list.List {...

*list is in lower case.

Same when I declared a local reference to a list - I had to use:

l := list.New()

Again, lower case for list.

So, I'm confused. What is the rule? The list calls and references are obviously public, or I wouldn't be able to call/use them - so why are they in lower case?

  • 写回答

3条回答 默认 最新

  • dongquechan4414 2013-12-31 01:02
    关注

    In this case, list is the name of the package, which you are importing via import "container/list", and its public members are upper case, like List.

    The rule is that public functions, types, etc., should be upper case.

    You can alias imported packages however you want, but by default it is just the name of the last part of the package path--in this case, list.

    Update: It's not the last part of the package path. It's the actual package name (which is often the same thing).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制