Its a taking time to adapt from Python to Golang and I am seeking help in reviewing my go code
Trying to convert a Python code to Golang. I have re-written code in Golang. Attached is the code screenshot. Kindly review my code and let me know the errors. Thanks
DO NOT translate any codes directly. Try to understand the algorithm/meaning before writing out using the targeted language in its own way. This applies to any languages both programming and linguistic in nature (not just Go alone).
DO NOT be a translator if your have not master even the basic of the targeted language. You will create a lot of burden for your team (potentially rewrite everything you did from scratch). Stick to Go package development and complete your Go tutorial to get some experience first. You have been warned!
Also, avoid using self as general practice (not a rule). Interface in Go is not OO so do not assign OO common terminologies for it.
3. Insufficient information for funcT2 output
The undocumented funcT2 did not specify the output type. On Go side, you cannot simply return nil if your function is not expecting to return any output.
func FuncT2(self, p1 string) { // did not expect any output
You need to query info from your python developer for FuncT2 output data type. If you really need to return nil, then the function should look something like:
func (x *CallmainFunc) FuncT2(p1 string) error { // assuming this is what is expected from Python side
return nil
}
Otherwise, a simply execution without return value is fine:
4. Insufficient information whether all the methods are exportable
You need to determine are those methods (funcT1, funcT2, …) meant to be exportable (accessible outside package) or otherwise. Go has a strict rule for it (notice I re-capitalized all your function taking the assumption that they are all exportable, which is an expectation from Python side). See (A Tour of Go). In short:
// This is exported function (starts with capital case letter) which is visible
func (x *CallmainFunc) FuncT3(t1 string) bool {
...
}
// This is internal function (starts with lowercase letter) which is private to that package itself
func (x *CallmainFunc) funcT3(t1 string) bool {
...
}
5. Insufficient Info for Init function
On Go side, are the keywords already processed before creating the structure? If yes, then create an exported New package function to create the structure object instead:
func New(kwargs... string) *CallmainFunc {
// process your kwargs (an array)
return &CallmainFunc {
// assign your values here. Example:
// Name: "John Smith",
}
}
However, since your init function on Python side does nothing and the assigned variable is not used anywhere, you can discard that function implementation entirely in Go, even with the New(...) function above. Reason:
Go compiler will not compile for unused variables.
New(...) function is redundant if the struct does not need any pre-processing. Your user can create the structure directly like &CallmainFunc{ ...} instead.
6. funcT4 is incomprehensible without documentation
funcT4 is not comprehensible without its documentations. Looks like work in progress spaghetti codes.
Notice that after z2, the triple quote string is open but not closed which means anything after those quotes are string in nature (commented out). Hence, I strongly suspect the function is work in progress. You need to consult the original developer.
Totally understand your difficulty in moving to new language. There are many good Go tutorials for beginners and strongly encourage to understand the basic concept and run the sample codes in play.golang.org. This helped me too