How to use constant across go package - go

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")
}

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"
}

Name resolution problem in multi package go module

I'm trying to create a module that contains multiple packages but I don't understand why I'm getting errors.
Layout:
λ ~/code/go-test/src/mod-test/ » tree
.
├── cmd
│   └── a.go
├── go.mod
├── main.go
└── pkg
└── foo
└── b.go
3 directories, 4 files
main.go:
package main
import (
"fmt"
"github.com/go-test/mod-test/cmd"
)
func main() {
fmt.Println("main")
A()
}
cmd/a.go:
package cmd
import (
"fmt"
"github.com/go-test/mod-test/pkg/foo"
)
func A() {
fmt.Println("a")
B()
}
pkg/foo/b.go:
package foo
import "fmt"
func B() {
fmt.Println("B")
}
go.mod:
module github.com/go-test/mod-test
go 1.12
I get the following error:
λ ~/code/go-test/src/mod-test/ » go build
# github.com/go-test/mod-test/cmd
cmd/a.go:5:2: imported and not used: "github.com/go-test/mod-test/pkg/foo"
cmd/a.go:10:2: undefined: B
Can anyone help explain what I've done wrong and why I get the error?
Thanks,
There is bug in the cmd/a.go. You did not use "github.com/go-test/mod-test/pkg/foo". Also B() is under github.com/go-test/mod-test/pkg/foo package, so you have to specify it. See below:
package cmd
import (
"fmt"
"github.com/go-test/mod-test/pkg/foo"
)
func A() {
fmt.Println("a")
// B()
foo.B()
}
There is another way to avoid this. If you don't want to use the package name, simply just put a . before importing a package. By doing that you can call a public fn or use a public var of that package. After doing that your main.go and cmd/a.go files look like below:
main.go:
package main
import (
"fmt"
. "github.com/go-test/mod-test/cmd"
)
func main() {
fmt.Println("main")
A()
}
cmd/a.go:
package cmd
import (
"fmt"
. "github.com/go-test/mod-test/pkg/foo"
)
func A() {
fmt.Println("a")
B()
}

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
}

Implementing an interface in 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
}

how to get correct file when I wrap a logger in go?

I use a global logger like this:
[root#dev log]# cat src/logging/logging.go
package logging
import (
"log"
"os"
)
var Logger *log.Logger
func init() {
Logger = log.New(os.Stdout, "[Debug]", log.Llongfile|log.LstdFlags)
}
func Debug(format string, v ...interface{}) {
Logger.SetPrefix("[Debug] ")
Logger.Printf(format, v...)
}
[root#dev log]# cat src/main/main.go
package main
import "logging"
func main() {
logging.Debug("in main")
}
Here in main function I want to get:
[Debug] 2015/12/10 22:20:23 /tmp/log/src/main/main.go:6: in main
But I get following output instead:
[Debug] 2015/12/10 22:20:23 /tmp/log/src/logging/logging.go:16: in main
How can I get the correct file which calling the logger?
Instead of using logger.Printf, you need to use the raw logger.Output function, and provide the correct callDepth for the call point (the default is 2).
If you look at the code for Logger.Printf, you can see how it's done by default:
func (l *Logger) Printf(format string, v ...interface{}) {
l.Output(2, fmt.Sprintf(format, v...))
}

Resources