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

gsf

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.

Kaedys

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Public/Private struct in Rust

From Dev

Does public and private have any influence on the memory layout of an object?

From Dev

Is there any way to use private functions in public macros in Rust?

From Dev

Is there any reason to use the "public" keyword before method and member variable names?

From Dev

Private implementation for public interface method

From Dev

Inner private class and public method

From Dev

why use final as modifier when we have private in method classes

From Dev

Public structs inheriting private struct C++

From Dev

Public structs inheriting private struct C++

From Dev

How to use struct in this case

From Dev

how to can i access a variable declared in a public method inorder to use it in a private method

From Dev

Default struct initialisation in any case?

From Dev

Dynamically create a public method in a private method

From Dev

Is there any purpose to make private class variable to public

From Dev

Why have public methods inside private classes?

From Dev

Golang, call method from struct

From Dev

Using struct method in Golang template

From Dev

How can a public method return a private type?

From Dev

Private class as return type from public method

From Dev

access private object variable from public method

From Dev

Effect of moving a method from public to private

From Dev

java call public method from private class

From Dev

How to make a private variable in a public method

From Dev

Public/private method signatures in Java interfaces

From Dev

Getting "public variable" in a "private method" using CoffeeScript

From Dev

Scope of public method in private class in java - no package

From Dev

Symfony2. How to ensure controller implements some method no matters if its public/private/protected and if it has any arguments?

From Dev

How to properly use struct in this case

From Dev

What is the use of tag in golang struct?

Related Related

  1. 1

    Public/Private struct in Rust

  2. 2

    Does public and private have any influence on the memory layout of an object?

  3. 3

    Is there any way to use private functions in public macros in Rust?

  4. 4

    Is there any reason to use the "public" keyword before method and member variable names?

  5. 5

    Private implementation for public interface method

  6. 6

    Inner private class and public method

  7. 7

    why use final as modifier when we have private in method classes

  8. 8

    Public structs inheriting private struct C++

  9. 9

    Public structs inheriting private struct C++

  10. 10

    How to use struct in this case

  11. 11

    how to can i access a variable declared in a public method inorder to use it in a private method

  12. 12

    Default struct initialisation in any case?

  13. 13

    Dynamically create a public method in a private method

  14. 14

    Is there any purpose to make private class variable to public

  15. 15

    Why have public methods inside private classes?

  16. 16

    Golang, call method from struct

  17. 17

    Using struct method in Golang template

  18. 18

    How can a public method return a private type?

  19. 19

    Private class as return type from public method

  20. 20

    access private object variable from public method

  21. 21

    Effect of moving a method from public to private

  22. 22

    java call public method from private class

  23. 23

    How to make a private variable in a public method

  24. 24

    Public/private method signatures in Java interfaces

  25. 25

    Getting "public variable" in a "private method" using CoffeeScript

  26. 26

    Scope of public method in private class in java - no package

  27. 27

    Symfony2. How to ensure controller implements some method no matters if its public/private/protected and if it has any arguments?

  28. 28

    How to properly use struct in this case

  29. 29

    What is the use of tag in golang struct?

HotTag

Archive