Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang public method to private struct - is this have any use case

I am new to go. I noticed in some libraries there are public methods to a private struct. See for example https://github.com/btcsuite/btcd/blob/master/txscript/stack.go

Does this have a real use case? I cannot picture how I will be able to use public methods, if I cannot access the structure they belong too.

As the following question - is there any way to overcome the private declaration of a struct? For example, I would like to use the structure stack from the pointed file in my package.

like image 974
gsf Avatar asked Jun 21 '16 18:06

gsf


People also ask

Can Go structs have methods?

Go supports methods defined on struct types. This area method has a receiver type of *rect . Methods can be defined for either pointer or value receiver types. Here's an example of a value receiver.

Does Go have private variables?

You can have private variables and functions in Go, but the trick is that you simply don't define them in the struct.

How do I create a private method in Golang?

In Golang, there are no Public and Private keywords. But you can distinguish Public and Private by using the uppercase and lowercase of the name. The Public function can be called by other packages, but the Private function can only be called by the package that defines it.

What are methods in Golang?

Go methods are similar to Go function with one difference, i.e, the method contains a receiver argument in it. With the help of the receiver argument, the method can access the properties of the receiver. Here, the receiver can be of struct type or non-struct type.


2 Answers

So, the major use case for this is that you can return an unexported type from a package function. Any exported methods can then be used on this type, even though other packages can't actually create the type in the first place (except for receiving it as a return value from your package).

package foobar

type foo struct {}
func (f foo) Bar() {}
func Newfoo() foo {
    return foo{}
}
----
package main
func main() {
    f := foobar.Newfoo()
    f.Bar()

    foobar.NewFoo().Bar()
}

The other major use case is in package-level interfaces. I tend to use a fairly standard pattern wherein a package defines an exported interface, like this:

type Doer interface {
    Do()
}

And then defines some private type(s) that fulfills that interface:

type stringDoer struct {}

func (s *stringDoer) Do() {}

func NewStringDoer() Doer {
    return &stringDoer{}
}

That way external packages can use my types as interfaces without having to mess around with concrete types, and since the exposed data types of my package are entirely interfaces, it makes mocking or stubbing calls to my package (example, for testing) incredibly easy.

For this type of system to work, the methods on the struct (in this case, stringDoer) must be exported, even if the struct itself isn't.

like image 126
Kaedys Avatar answered Oct 16 '22 01:10

Kaedys


This is the commit that unexports it: https://github.com/btcsuite/btcd/commit/7411e65b1e8d4e4c6da198590ad627bfe253173c. From the commit message:

This commit unexports the Stack type since it is only intended to be used internally during script execution. Further, the engine exposes the {G,S}etStack and {G,S}etAltStack functions which return the items as a slice of byte slices ([][]byte) for caller access while stepping.

I believe you're correct that you (outside the package) cannot make use of this struct. It's used purely inside the package implementation.

like image 43
user94559 Avatar answered Oct 16 '22 00:10

user94559