Go id for structure in package?

I have a package named "foo" because it defines the Foo structure and related things like methods.

The package has a New method that returns the Foo struct selected by the heap.

Can the structure type be called Foo when the package name is "foo"? I'm not sure because it stutters.

var f foo.Foo

On the other hand, it’s natural to write

f := foo.New(...)

+4
source share
1 answer

This is normal, and it is also idiomatic.

Similar examples from the standard library:

, , foo.Foo - , , , . , http.CookieJar, net/http/cookiejar, , http.CookieJar, cookiejar.Jar, cookiejar.CookieJar.

( ). foo.Foo . , . , - , , foo.F . , bson.M bson.D, , MongoDB. , , , .

+5

Source: https://habr.com/ru/post/1689119/


All Articles