How to write nice and clean Go package?

Multi tool use
I try to implement Sparse sets from this article in Go and make it into a package. In early implementation, the API is clean and minimal with only exported type Sparse
and exported method Insert
, Delete
, Has
, Union
, Intersection
, Clear
, and Len
, pretty much only basic sets operation.
Later on, I want to add new functionality, sets that can reserve an element automatically (lets call it AutoSparse
). If Sparse
have Insert(k int)
which insert k into sets, AutoSparse
have Reserved()
reserved an available element. If I have 0 1 2 4 5
in AutoSparse
, when i call Reserve()
it must add 3, not 6 so now it become 0 1 2 4 5 3
. Here's the implementation at playground.
As you can see, in order to maintain which element to added into sets, it doesn't add new field in the struct
and I want to keep it like that.
My Question is how to add that new functionality to my package without adding new exported type AutoSparse
to keep the API clean and minimal?
This is what i already try:
- I can use
interface
to hide implementation but function signature is different, one useInsert(k int)
, the other useReserve()
, even if I use nameInsert()
it still different, or should I useInsert(k int)
but didn't usek
at all? it can but it's awkward. - I can't use the same
struct
to implement this because once you UseReserve()
to add element, you can't useInsert(k int)
because it will messed up the reserved element, evenDelete
andClear
is different.
go package
add a comment |
I try to implement Sparse sets from this article in Go and make it into a package. In early implementation, the API is clean and minimal with only exported type Sparse
and exported method Insert
, Delete
, Has
, Union
, Intersection
, Clear
, and Len
, pretty much only basic sets operation.
Later on, I want to add new functionality, sets that can reserve an element automatically (lets call it AutoSparse
). If Sparse
have Insert(k int)
which insert k into sets, AutoSparse
have Reserved()
reserved an available element. If I have 0 1 2 4 5
in AutoSparse
, when i call Reserve()
it must add 3, not 6 so now it become 0 1 2 4 5 3
. Here's the implementation at playground.
As you can see, in order to maintain which element to added into sets, it doesn't add new field in the struct
and I want to keep it like that.
My Question is how to add that new functionality to my package without adding new exported type AutoSparse
to keep the API clean and minimal?
This is what i already try:
- I can use
interface
to hide implementation but function signature is different, one useInsert(k int)
, the other useReserve()
, even if I use nameInsert()
it still different, or should I useInsert(k int)
but didn't usek
at all? it can but it's awkward. - I can't use the same
struct
to implement this because once you UseReserve()
to add element, you can't useInsert(k int)
because it will messed up the reserved element, evenDelete
andClear
is different.
go package
add a comment |
I try to implement Sparse sets from this article in Go and make it into a package. In early implementation, the API is clean and minimal with only exported type Sparse
and exported method Insert
, Delete
, Has
, Union
, Intersection
, Clear
, and Len
, pretty much only basic sets operation.
Later on, I want to add new functionality, sets that can reserve an element automatically (lets call it AutoSparse
). If Sparse
have Insert(k int)
which insert k into sets, AutoSparse
have Reserved()
reserved an available element. If I have 0 1 2 4 5
in AutoSparse
, when i call Reserve()
it must add 3, not 6 so now it become 0 1 2 4 5 3
. Here's the implementation at playground.
As you can see, in order to maintain which element to added into sets, it doesn't add new field in the struct
and I want to keep it like that.
My Question is how to add that new functionality to my package without adding new exported type AutoSparse
to keep the API clean and minimal?
This is what i already try:
- I can use
interface
to hide implementation but function signature is different, one useInsert(k int)
, the other useReserve()
, even if I use nameInsert()
it still different, or should I useInsert(k int)
but didn't usek
at all? it can but it's awkward. - I can't use the same
struct
to implement this because once you UseReserve()
to add element, you can't useInsert(k int)
because it will messed up the reserved element, evenDelete
andClear
is different.
go package
I try to implement Sparse sets from this article in Go and make it into a package. In early implementation, the API is clean and minimal with only exported type Sparse
and exported method Insert
, Delete
, Has
, Union
, Intersection
, Clear
, and Len
, pretty much only basic sets operation.
Later on, I want to add new functionality, sets that can reserve an element automatically (lets call it AutoSparse
). If Sparse
have Insert(k int)
which insert k into sets, AutoSparse
have Reserved()
reserved an available element. If I have 0 1 2 4 5
in AutoSparse
, when i call Reserve()
it must add 3, not 6 so now it become 0 1 2 4 5 3
. Here's the implementation at playground.
As you can see, in order to maintain which element to added into sets, it doesn't add new field in the struct
and I want to keep it like that.
My Question is how to add that new functionality to my package without adding new exported type AutoSparse
to keep the API clean and minimal?
This is what i already try:
- I can use
interface
to hide implementation but function signature is different, one useInsert(k int)
, the other useReserve()
, even if I use nameInsert()
it still different, or should I useInsert(k int)
but didn't usek
at all? it can but it's awkward. - I can't use the same
struct
to implement this because once you UseReserve()
to add element, you can't useInsert(k int)
because it will messed up the reserved element, evenDelete
andClear
is different.
go package
go package
edited Nov 13 '18 at 13:31
Flimzy
38.5k106597
38.5k106597
asked Nov 13 '18 at 11:48


billyzaelanibillyzaelani
10912
10912
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use k... int
as parameter.
func (sparse Sparse) Insert(k... int) error
if len(k) == 0
// first case
return nil
else if len(k) == 1
// second case
return nil
else
return errors.New("Too many arguments")
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280394%2fhow-to-write-nice-and-clean-go-package%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use k... int
as parameter.
func (sparse Sparse) Insert(k... int) error
if len(k) == 0
// first case
return nil
else if len(k) == 1
// second case
return nil
else
return errors.New("Too many arguments")
add a comment |
You can use k... int
as parameter.
func (sparse Sparse) Insert(k... int) error
if len(k) == 0
// first case
return nil
else if len(k) == 1
// second case
return nil
else
return errors.New("Too many arguments")
add a comment |
You can use k... int
as parameter.
func (sparse Sparse) Insert(k... int) error
if len(k) == 0
// first case
return nil
else if len(k) == 1
// second case
return nil
else
return errors.New("Too many arguments")
You can use k... int
as parameter.
func (sparse Sparse) Insert(k... int) error
if len(k) == 0
// first case
return nil
else if len(k) == 1
// second case
return nil
else
return errors.New("Too many arguments")
answered Nov 13 '18 at 14:52


Maxim PanfilovMaxim Panfilov
160111
160111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280394%2fhow-to-write-nice-and-clean-go-package%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
F0Zodt1x zVGU,F28FSyKDKzKtVD,ixDCBm,aD0Kje kDOlMLsPTj HT1wxTjCw52V3xM