Implementing an interface in Go - go

My project is organized as follows
github.com/achanda/poke
├── cmd
│   └── poke.go
├── scanner.go
├── txt_scanner.go
└── types.go
The files are as follows
# cat scanner.go
package poke
type Scanner interface {
Scan() *ScanResult
}
# cat txt_scanner.go
package poke
type txtScanner struct {
txt string
}
func newTxtScanner(host string) Scanner {
return txtScanner{txt}
}
func (tcpcs txtScanner) Scan() *ScanResult {
// do stuff
return &result
}
Now I am trying to call this in my main package (in poke.go) like this
package main
import "github.com/achanda/poke"
func main() {
var sr poke.Scanner
sr = poke.txtScanner{txt}
sr.Scan()
}
This fails to run with
# command-line-arguments
./poke.go:111: cannot refer to unexported name poke.txtScanner
./poke.go:111: undefined: portscan.txtScanner
What am I doing wrong?

you need to access type or field outside package, so you should export them using first letter upper case:
first you should define your txtScanner and txt string with first upper case letter, otherwise you will see this error too:
.\poke.go:8: implicit assignment of unexported field 'txt' in poke.TxtScanner literal
like this:
type TxtScanner struct {
Txt string
}
also see newTxtScanner(host string) function in this working sample codes:
poke.go:
package main
import "github.com/achanda/poke"
func main() {
s := "test"
var sr poke.Scanner
sr = poke.TxtScanner{s}
sr.Scan()
}
txt_scanner.go:
package poke
type TxtScanner struct {
Txt string
}
func newTxtScanner(host string) Scanner {
return TxtScanner{host}
}
func (tcpcs TxtScanner) Scan() *ScanResult {
// do stuff
result := ScanResult{}
return &result
}
types.go:
package poke
type ScanResult struct {
}
scanner.go:
package poke
type Scanner interface {
Scan() *ScanResult
}

Related

passing map between packages in golang

In golang, I know that map is passed between functions in the form of reference, but I encountered a strange situation today. The running result of the code is not what I imagined. I simplified it into the following lines of code.
.
├── go.mod
├── main.go
├── packageA
│   └── a.go
└── packageB
└── b.go
main.go file
package main
import (
"gostudy/packageA"
"gostudy/packageB"
)
func main() {
packageB.UseMap(packageA.M, packageA.InitMap)
}
a.go
package packageA
var M map[string]string
func InitMap() {
M = make(map[string]string)
M["hello"] = "go"
}
b.go
package packageB
import "fmt"
func UseMap(m map[string]string, callback func()) {
callback()
fmt.Println(m)
}
As you can see, there is only one variable globally declared in the a.go file. I thought the above program should output map[hello:go], but it actually outputs an empty map[]. I'm very confused about this and hope to get an answer.
You're passing the old value of the map as a parameter, before you invoke the function to replace it with a new version of the map.
Let's say packageA.M contains the value map[string]string{"foo": "bar"}. The main() function reads the variable and gets a reference to this map, and passes it and the function to packageB.UseMap().
Inside packageB.UseMap(), your code calls packageA.InitMap() via the callback. This does not modify the existing map; instead, it creates a new map, assigns it to the global variable, and populates it. Anything that had a copy of the old map is unaffected, and the code you show doesn't re-read the value of packageA.M.
I'd recommend dispensing with the global variable entirely: it can make the code hard to test and there are potential problems once you start using goroutines. Just have your setup function return the new map.
package packageA
func InitMap() map[string]string {
return map[string]string{"hello": "go"}
}
package packageB
func UseMap(callback func() map[string]string) {
m := callback()
fmt.Println(m)
}
package main
import "packageA"
import "packageB"
func main() {
packageB.UseMap(packageA.InitMap)
}
Just as a side note to the accepted anwer, if you take a look at this:
// ...
import (
"reflect"
"fmt"
)
// ... other functions
// I defined all of the functions in a single paackage, so I can access them both here
func UseMap(m map[string]string, callback func()) {
fmt.Println(reflect.ValueOf(m).Pointer() == reflect.ValueOf(M).Pointer()) // prints true, they have the same reference
callback()
// inside callback, M global variable takes a whole new reference
// when "M = make(...)"
// and then =>
fmt.Println(reflect.ValueOf(m).Pointer() == reflect.ValueOf(M).Pointer()) // prints false
}
If you want to avoid this without changing your apis, you can do this in your package A:
package packageA
var M map[string]string = make(map[string]string)
func InitMap() {
M["hello"] = "go"
}

How to pass anonymous struct as function argument

The problem is that I can't access struct properties inside Converter function, but I can print em.
func main() {
var io struct {
Src string
Dest string
}
flag.StringVar(&io.Src, "src", "temp_dir", "")
flag.StringVar(&io.Dest, "dest", "users_dir", "")
modules.Converter(&io)
}
// ./src/modules/converter.go
package modules
func Converter(io interface{}) {
fmt.Println(io)
// => {temp_dir users_dir}
}
What am I doing wrong?
What is the right way to pass multiple props into function?
first, you need to change the io struct to Io
var Io struct {
Src string
Dest string
}
And the function input should change this way
package modules
import "fmt"
func Converter(io *namePackage.Io) {
fmt.Println(io.Src,io.Dest)
}

Method that is present being shown as undefined golang

I am trying to call a method from a different directory but getting an error saying that the method is not present. I have the method present with the first letter Uppercase.
I have the following directory structure
[laptop#laptop src]$ tree
.
├── hello
│   ├── hello.go
├── remote_method
│   └── remoteMethod.go
My main is in hello.go and tries to call a function from the remote_method package
package main
import
(
"remote_method"
)
func main() {
mm := remote_method.NewObject()
mm.MethodCall()
}
The remoteMethod.go has the following contents
package remote_method
import (
.....
)
type DeclaredType struct {
randomMap (map[string][](chan int))
}
func NewObject() DeclaredType {
var randomMap (map[string][](chan int))
m := DeclaredType{randomMap}
return m
}
func MethodCall(m DeclaredType, param1 string, param2 string, param3 string, param4 string) {
// Code to be run
}
I get the error
mm.MethodCall undefined (type remote_method.DeclaredType has no field or method MethodCall)
Can someone help me in finding why the method is not visible or any possible ways I could find that.
TIA
Register MethodCall() as a receiver in DeclaredType.
remote_method.go
package remote_method
import (
.....
)
type DeclaredType struct {
randomMap (map[string][](chan int))
}
func NewObject() DeclaredType {
var randomMap (map[string][](chan int))
m := DeclaredType{randomMap}
return m
}
func (d DeclaredType) MethodCall(m DeclaredType, param1 string, param2 string, param3 string, param4 string) {
// Code to be run
}

How to use constant across go package

I'm writing my first go code and I'm trying to convince myself what I'm doing is not wrong.
anyway, here the project the tree structure.
.
├── helpers
│   └── common.go
├── logger
│   └── util.go
├── logger_example
└── runner.go
The main file to look over here is logger/util.go which look like this.
package logger
import (
"log"
"os"
)
type Logger struct {
*log.Logger
}
func (l *Logger) Info(v ...interface{}) {
l.SetPrefix("Info: ")
l.Println(v...)
}
func (l *Logger) Error(v ...interface{}) {
l.SetPrefix("Error: ")
l.Println(v...)
}
func (l *Logger) Warn(v ...interface{}) {
l.SetPrefix("Warn: ")
l.Println(v...)
}
func (l *Logger) Debug(v ...interface{}) {
l.SetPrefix("Debug: ")
l.Println(v...)
}
func NewLogger() *Logger {
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
return &Logger{logger}
}
As you all can see, I'm just setting the prefix to "INFO | WARN | ERROR | DEBUG"
On the main package I have safely created a Logger instance and VOILA it worked.
Only until I decided to add a helpers package and now things do not look the way I wanted.
since the logger.Logger instance created inside main package, I have to pass it reference to every since package function where I want to invoke the logger statement. (see the below example ..)
// dodly_logger/logger/common.go
package helpers
import "dodly_logger/logger"
func Display(dodlyLogger *logger.Logger) {
dodlyLogger.Info("Inside a helper package")
}
The Main package..
package main
import (
logger "dodly_logger/logger"
helpers "dodly_logger/helpers"
)
func main() {
dodlyLogger := logger.NewLogger()
dodlyLogger.Info("INFO MESSAGE")
dodlyLogger.Error("ERROR MESSAGE")
// Ehh, I have to pass the dodlyLogger ..
helpers.Display(dodlyLogger)
}
Ok, now I know my GOLang knowledge is not complete hence I'm hoping people over here can point me how can I write this more clinically wherein I do not have to pass the reference of the logger.Logger to every function for which I need to log.
Create a package level variable with var.
package main
import (
logger "dodly_logger/logger"
helpers "dodly_logger/helpers"
)
var dlogger logger.Logger
func init() {
dlogger = logger.NewLogger()
}
func main() {
dlogger.Info("Starting Main")
a()
}
func a() {
dlogger.Info("in a")
}

Create constructor for different go structs

I have a struct which I initiate in some process like following, and this is working as expected.
This is specific runner
type TestRunner struct {
path string
name string
}
func NewRunner(p string, n string) *TestRunner {
return &TestRunner{
path: p,
name: n,
}
}
Now I want in the same package to create another runner so I do it like this e.g.
Also specific runner
type TestRunner2 struct {
path string
name string
}
func NewRunner(p string, n string) *TestRunner2 {
return &TestRunner2{
path: p,
name: n,
}
}
Now I get error that the func NewRunner is exist
I have another file (in the same package) which include the interface
This is generic implementation (different file in the same package)
type Runner interface {
Run(path string) error
ChangePath(newPath string)
}
So maybe the NewRunner should be there, where its recommended to put the new object ?
obviously I can create NewRunner1 and NewRunner2 method in the file but im not sure if it’s recommended
First, you should name your runners according to their functionality, not by number. FastRunner and SlowRunner or LocalRunner vs RemoteRunner. You get the idea. Then you should create a construct for each one:
func NewFastRunner( ... ) *FastRunner {
return &FastRunner{ ... }
}
func NewSlowRunner( ... ) *SlowRunner {
return &SlowRunner{ ... }
}
This is standard practice, and makes for very readable, unambiguous code.
You can use method pointer type receivers for each runner and then implement interface. That way you need not to return any thing you can directly assign values like path and name to runner using pointer.
package main
import (
"fmt"
)
type TestRunner1 struct {
path string
name string
}
type TestRunner2 struct {
path string
name string
}
type Runner interface {
Run(path string) error
ChangePath(newPath string)
}
func (tr1 *TestRunner1) NewRunner(p string, n string) {
tr1.path = p
tr1.path = n
}
func (tr2 *TestRunner2) NewRunner(p string, n string) {
tr2.path = p
tr2.path = n
}
func main() {
fmt.Println("Hello, playground")
}
Check the code here

Resources