How to distingish between package name and object name in golang

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

How to distingish between package name and object name in golang

问题

以下是翻译好的代码部分:

package main
import (
......
"fmt"
......
)

func main() {
    ......
    xxx := new(xxx)
    fmt.Println("1")
    ......
    xxx.Println() //令人困惑

}
type xxx struct{
    one int
    two string
}
func (yyy *xxx) Println(){
    fmt.Println("2")
    yyy.Print(3) //同样令人困惑
}
func (this *xxx) Print(a int){
    fmt.Println(a)
}

这个问题困扰着我,如何快速判断一个标识符是一个包名还是一个对象名,就像上面的代码中,在main函数中,fmt被认为是一个包名,因为大家都知道。但是当遇到xxx.Println()时,你如何知道它是一个包名还是一个对象名,假设main函数包含很多行代码,很难准确定位对象"xxx"的声明,并且有很多导入的包,你很难搜索每一行import()来查看"xxx"是否在包中列出,你如何判断xxx是什么?

对于函数Println,你直接调用yyy.Print(3),函数声明中的接收者很难找到,因为它超出了屏幕范围,你如何快速判断yyy是什么,yyy既可以是包名,也可以是接收者名字。

如果没有更简单的方法,那是否意味着我应该总是先搜索import()列出的包,然后滚动屏幕到函数声明的开头找到接收者的名字,然后才能知道它是什么?这听起来太费时间了!

英文:
package main
import (
......
"fmt"
......
)

func main() {
        ......
    xxx:=new(xxx)
    fmt.Println("1")
        ......
    xxx.Println()//confusing

}
type xxx struct{
    one int
    two string
}
func (yyy *xxx) Println(){
    fmt.Println("2")
    yyy.Print(3)//confusing too
}
func (this *xxx) Print(a int){
    fmt.Println(a)
}

This question is hunted me,what is the fastest way to tell whether it is a package name or an object name,as the code presented above,in the main func,fmt is known as a package name,because everybody knows.But when comes to xxx.Println(),how would you know whether it's a package name or an object name,assuming that the main function contains many lines of code,and it is hard to pin point the declaration of object "xxx",and,there are many packages imported and it is hard for you to search for every line of import() to see if "xxx" is listed in the packages,how do you tell what xxx is?

Same as func Println,you just come straight forward to yyy.Print(3),the receiver in the func declaration is hard to find because it is out of the screen,how would you tell what yyy is the fastest way,yyy could be either the package name and the receiver name.

If there is no such an easier way,does that mean I should always search for the import() listed packages first,and scroll the screen to the very beginning of the func declaration to find what is the receiver name,and then I can know what it is?That sounds to take too much time!

答案1

得分: 2

对于第二个问题(Println() 方法内的 yyy.Println()),我认为使用非常短的名称来命名接收器是标准的 Go 语言风格。因此,标准的 Go 语言风格建议:

func (x *xxx) Println() {
    fmt.Println(2)
    x.Print(3)
}

至于第一个问题……我不确定你是在谈论你自己的代码还是别人的代码。我认为了解给定程序或库使用了哪些模块是相当标准的。(非常短的变量名和驼峰命名法变量名也有帮助。)

英文:

For the second question (yyy.Println() inside method Println()), I think it’s standard Go style to name your receivers with very short names. So, standard Go style would suggest:

func (x *xxx) Println() {
    fmt.Println(2)
    x.Print(3)
}

As for the first question… I’m not sure if you’re talking about your code, or somebody else’s code. I would say it’s pretty standard that you eventually learn about what modules a given program or library is using. (Really short variable names, and then camelCase variable names also help.)

答案2

得分: 1

使用短期记忆,快速扫描源代码以查找显著特征。

package main

import (
	// ......
	"fmt"
	// ......
)

type xxx struct {
	one int
	two string
}

func (this *xxx) Print(a int) {
	fmt.Println(a)
}

func (yyy *xxx) Println() {
	fmt.Println("2")
	yyy.Print(3) // yyy 是接收者 *xxx
}

func main() {
	// ......
	xxx := new(xxx)
	fmt.Println("1")
	// ......
	xxx.Println() // xxx 是变量 xxx
}

应用语言规则,例如块和作用域规则。

通过实践,这很容易。

参考资料:

Go编程语言规范

导入声明

限定标识符

选择器

声明和作用域

英文:

Using short-term memory, quickly scan the source code for salient features.

package main

import (
	// ......
	"fmt"
	// ......
)

type xxx struct {
	one int
	two string
}

func (this *xxx) Print(a int) {
	fmt.Println(a)
}

func (yyy *xxx) Println() {
	fmt.Println("2")
	yyy.Print(3) // yyy is receiver *xxx
}

func main() {
	// ......
	xxx := new(xxx)
	fmt.Println("1")
	// ......
	xxx.Println() // xxx is var xxx
}

Apply the language rules. For example, block and scope rules.

With practice, it's easy.

References:

The Go Programming Language Specification

Import declarations

Qualified identifiers

Selectors

Blocks

Declarations and scope

huangapple
  • 本文由 发表于 2014年4月6日 13:57:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/22890511.html
匿名

发表评论

匿名网友

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

确定