How to solve the import cycle using an interface in Golang? - go

I have encountered an import cycle problem #icza and I am trying to use the interface to solve this but after going through some resources, I still didn't understand properly and it is giving me an error that
Error
runtime: goroutine stack exceeds 1000000000-byte limit runtime: sp=0xc020160360 stack=[0xc020160000, 0xc040160000] fatal error: stack overflow
file1.go
package dir1
type Functions interface {
Second(string) string
}
func First(f Functions, message string) string {
importFile2 := f.Second("second file")
return "Welcome to " + message + " and message from file 2: " + importFile2
}
file2.go
package dir2
import "github.com/ibilalkayy/app/dir1"
type SecondT struct{}
func (SecondT) Second(message string) string {
importFile1 := dir1.First(SecondT{}, "first file")
return "Welcome to " + message + " and message from file 1: " + importFile1
}
main.go
package main
import (
"fmt"
"github.com/ibilalkayy/app/dir1"
"github.com/ibilalkayy/app/dir2"
)
func main() {
fmt.Println("Hello world")
first := dir1.First(dir2.SecondT{}, "first file")
fmt.Println(first)
}

Related

Print User input to physical printer in Golang

I'm new here in Golang world for a week.. I've been research and still don't know how to print from user input to physical printer, from USB to printer.
For example I have done with code, just need add other function to print..
package main
import "fmt"
func main() {
fmt.Println("First name: ")
var name1 string
fmt.Scanln(&name1)
)
fmt.Println("Last name: ")
var name2 string
fmt.Scanln(&name2)
fmt.Print("My full name is: ")
fmt.Print(name1 + " " + name2)
}
func print(){
//need function to print directly from USB to pyhsical printer..
//print from func main() as text, any format
}
I have no idea to create print func in golang from user input..
If successful, then after func main() println, then immediately send the command to print to my printer.. just using terminal, I don't need the GUI.
Thank you for helping me:')

Go Println suppressing stack-trace for panic in String()

In the code below, the invalid index access x[10] results in a panic. However, the panic message produced doesn't show the stack-trace of the error - rather, it looks like this: %!v(PANIC=String method: runtime error: index out of range [10] with length 3). Also, instead of terminating, the program keeps running after the panic occurs.
Based on this answer it seems that Println catches panics from String() methods and logs them. How do I prevent this behavior so that 1) my program terminates in the even of a panic in a String() method and 2) the full stack-trace of the panic is shown?
package main
import (
"fmt"
)
type Foo struct {
}
func (foo Foo) String() string {
var x = "123"
return fmt.Sprintf("%v", x[10]) // invalid index
}
func main() {
fmt.Println(Foo{})
fmt.Println("done")
}
Simply print the result of .String()
package main
import (
"fmt"
)
type Foo struct {
}
func (foo Foo) String() string {
var x = "123"
return fmt.Sprintf("%v", x[10])
}
func main() {
fmt.Println(Foo{}.String())
fmt.Println("done")
}

Printing value along with it's data type

package main
import "fmt"
func main() {
anInt := 1234
fmt.Printf("Data Type:", "%T\n", anInt, "Value is:", anInt)
}
Ouput:
Data Type:%!(EXTRA string=%T
, int=1234, string=Value is:, int=1234)
But Expected Output:
Data Type: int, Value is: 1234
I have tried using import reflect still not the expected result
Data Type:%!(EXTRA *reflect.rtype=int, string=Value is:, int=1234)
Package fmt
import "fmt"
func Printf
func Printf(format string, a ...interface{}) (n int, err error)
Printf formats according to a format specifier and writes to standard
output. It returns the number of bytes written and any write error
encountered.
It's a single format string. For example,
package main
import "fmt"
func main() {
anInt := 1234
fmt.Printf("Data Type: %T\nValue is: %v\n", anInt, anInt)
// or, concise version
fmt.Printf("Data Type: %[1]T\nValue is: %[1]v\n", anInt)
}
Output:
Data Type: int
Value is: 1234
Data Type: int
Value is: 1234

main.go:9: use of package str without selector

I have the following in the intepreter of Tour of Go:
package main
import (
"golang.org/x/tour/wc"
str "strings"
)
func WordCount(s string) map[string]int {
results := make(map[str]int)
words := str.Fields(s)
return map[string]int{"x": 1}
}
//func main() {
// wc.Test(WordCount)
//}
This is based on https://tour.golang.org/moretypes/23
My error is
tmp/sandbox169629521/main.go:9: use of package str without selector
Trying
results := make(map[str.string]int)
now fails with
tmp/sandbox424441423/main.go:9: cannot refer to unexported name strings.string
tmp/sandbox424441423/main.go:9: undefined: strings.string
"string" is a builtin. You don't need to do strings.string:
Docs: https://golang.org/pkg/builtin/#string

Convert data to base64 encode in go

I am new to go programming language and I'm stock on this scenario on my code.
Here's my example code:
a := genreAPI{Genre{"Pop"}, Genre{"Rock"}}
fmt.Println("Value of a :", a)
The current output is: Value of a : [{Pop} {Rock}]
How can I achieved an output like this:
Value of a : [{UG9w} {Um9jaw==}]
which is a base64 encode?
I am not sure what exactly is not clear from the documentation. Not only it has a clear name which explains states what the method is doing, it also has an example.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
data := []byte("Pop")
str := base64.StdEncoding.EncodeToString(data)
fmt.Println(str) // UG9w
}
Go Playground
You can customise the output of print functions by providing a String() method for your type. Either for the whole Genre or just for the name variable.
Example:
package main
import (
"encoding/base64"
"fmt"
)
type Base64String string
func (b Base64String) String() string {
return base64.StdEncoding.EncodeToString([]byte(b))
}
type Genre struct {
Name Base64String
}
func main() {
a := []Genre{Genre{"Pop"}, Genre{"Rock"}}
fmt.Println(a) // prints [{UG9w} {Um9jaw==}]
fmt.Println(string(a[0].Name)) // prints Pop
}

Resources