Designing Go code structure with sub-packages

-4

I'm getting confused trying to refactor computation inside a package into 3 stages of pre, process and post.


One idea was to develop 3 new sub-packages with this approach:

Refactor computation inside a package into 3 stages

The problem is I have to repeat some code in each sub-package as commented here. Also, after refactoring I'm receiving runtime errors which might indicate that the refactor has not been a stable one:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x58 pc=0x88e36e]

goroutine 1 [running]:
projects/printer/app/threed/support/pre.(*ASupportPre).volumeCalc(...)

In C++ you can do whatever you want:

  • Create separate classes in sub-directories
  • Design classes according to what they do and use composition
  • Design classes according to what they are and use inheritance

I wonder, in Go, how I can break down computation in my package into 3 stages. Is there a best practice which I might be missing?


Code

My original package:

package support

type ASupport struct {
    tris          tri.Tris
    boundary      *boundary.Boundaries
    // ...
}

// Functions with `ASupport` as receiver

func (s *ASupport) volumeCalc() {
    // ...
}

func (s *ASupport) PutSupport(a Sup) {
    // ...
}

// ...

I intend to refactor the functions with *ASupport type as receiver, into 3 sub-packages of pre, process and post according to the computation stage they belong to.

go
asked on Stack Overflow Apr 21, 2020 by user3405291 • edited Apr 21, 2020 by user3405291

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0