GoLang mismatched types []string and string - http

i am trying to write http api on GoLang. When i am comparing 2 string, i am taking this error "invalid operation: a.TypeI == m["type"][0] (mismatched types []string and string)". How can i fix, can anyone help ?
func listHandler(w http.ResponseWriter, r *http.Request) {
u, errUrl := url.Parse(r.URL.String())
check(errUrl)
m, _ := url.ParseQuery(u.RawQuery)
dat, err := ioutil.ReadFile("data.json")
check(err)
var basedata BaseData
err2 := json.Unmarshal(dat, &basedata)
check(err2)
for _, a := range basedata.Pokemons {
if a.TypeI == m["type"][0] || a.TypeII == m["type"][0] {
fmt.Fprintln(w, "dosomething")
}
}
}

Looking at the Go docs for the url package, you want Values.Get:
if a.TypeI == m.Get("type") || a.TypeII == m.Get("type") {
// do something
}
Furthermore, can you print the value of r.URL.String() for me like this:
fmt.Println(r.URL.String())

Related

How to assign key,value to map[interface{}]interface{}?

var cache atomic.Value
func setResToCache(res *utils.InterfaceMap) error {
resMap := res.ToInterfaceMap()
val := resMap[constant.key] // constant.key is a constant string
val, ok := val.(string)
if !ok {
return errors.New("type assertion failed")
}
someRes := model.someRes{
Title: val,
}
Cache.Store(someRes)
return nil
}
about utils.InterfaceMap
type InterfaceMap sync.Map
//ToInterfaceMap
func (im *InterfaceMap) ToInterfaceMap() map[interface{}]interface{} {
iim := make(map[interface{}]interface{})
m := (*sync.Map)(im)
m.Range(func(k, v interface{}) bool {
iim[k] = v
return true
})
return iim
}
I have some code similar to the above, and I have a problem when I want to write a unit test.
{
name: "test",
args: args{
res: &utils.InterfaceMap{
// How to assign k,v here
},
},
wantErr: false,
},
How to how to assign key,value to map[interface{}]interface{}?
It is actually map[string]string, so I use assert, but the parameter passed in is map[interface{}]interface{}.
I want to add some string: string to map so that I can successfully test the code.
Create simple function in your test file to input map[string]string and inside the function, write those values to sync.Map and cast it to InterfaceMap type and return the reference. Example is as below.
func createMockInterfaceMap(in map[string]string) *utils.InterfaceMap{
mockMap := sync.Map{}
for key, value := range in {
mockMap.Store(key, value)
}
iMap := utils.InterfaceMap(mockMap)
return &iMap
}
{
name: "test",
args: args{
res: createMockInterfaceMap(map[string]string{
`key1`:`value1`,
`key2`:`value2`,
}),
},
wantErr: false,
},
To pass in a map[interface{}]interface{}, it will need to be defined as such.
You can convert a map[string]string to a map[interface{}]interface{} using something like the following:
func MapConvert(mss map[string]string) map[interface{}]interface{} {
ifaceMap := map[interface{}]interface{}{}
for k, v := range mss {
ifaceMap[k] = v
}
return ifaceMap
}
Here's an example using a type definition:
type InterfaceMap map[interface{}]interface{}
func MapConvert(mss map[string]string) *InterfaceMap {
ifaceMap := InterfaceMap{}
for k, v := range mss {
ifaceMap[k] = v
}
return &ifaceMap
}
Here's a full running example:
https://play.golang.org/p/UPKqqZnFis-

How to convert interface{} to map

I am trying to create a function that could accept following
*struct
[]*struct
map[string]*struct
Here struct could be any struct not just a specific one.
Converting interface to *struct or []*struct is working fine.
But giving error for map.
After reflect it shows it is map[] but giving error when try to iterate over range.
Here is code
package main
import (
"fmt"
"reflect"
)
type Book struct {
ID int
Title string
Year int
}
func process(in interface{}, isSlice bool, isMap bool) {
v := reflect.ValueOf(in)
if isSlice {
for i := 0; i < v.Len(); i++ {
strct := v.Index(i).Interface()
//... proccess struct
}
return
}
if isMap {
fmt.Printf("Type: %v\n", v) // map[]
for _, s := range v { // Error: cannot range over v (type reflect.Value)
fmt.Printf("Value: %v\n", s.Interface())
}
}
}
func main() {
b := Book{}
b.Title = "Learn Go Language"
b.Year = 2014
m := make(map[string]*Book)
m["1"] = &b
process(m, false, true)
}
Is there any way to convert interface{} to map and iterate or get it's elements.
If the map value can be any type, then use reflect to iterate through the map:
if v.Kind() == reflect.Map {
for _, key := range v.MapKeys() {
strct := v.MapIndex(key)
fmt.Println(key.Interface(), strct.Interface())
}
}
playground example
If there's a small and known set of struct types, then a type switch can be used:
func process(in interface{}) {
switch v := in.(type) {
case map[string]*Book:
for s, b := range v {
// b has type *Book
fmt.Printf("%s: book=%v\n" s, b)
}
case map[string]*Author:
for s, a := range v {
// a has type *Author
fmt.Printf("%s: author=%v\n" s, a)
}
case []*Book:
for i, b := range v {
fmt.Printf("%d: book=%v\n" i, b)
}
case []*Author:
for i, a := range v {
fmt.Printf("%d: author=%v\n" i, a)
}
case *Book:
fmt.Ptintf("book=%v\n", v)
case *Author:
fmt.Printf("author=%v\n", v)
default:
// handle unknown type
}
}
You don't need reflect here. Try:
v, ok := in.(map[string]*Book)
if !ok {
// Can't assert, handle error.
}
for _, s := range v {
fmt.Printf("Value: %v\n", s)
}
Same goes for the rest of your function. It looks like you're using reflection when you would be better served by a type switch.
Alternatively, if you insist on using reflection here (which doesn't make a lot of sense) you can also use Value.MapKeys with the result from your ValueOf (see the answer https://stackoverflow.com/a/38186057/714501)
This may help:
b := []byte(`{"keyw":"value"}`)
var f interface{}
json.Unmarshal(b, &f)
myMap := f.(map[string]interface{})
fmt.Println(myMap)
Another way to convert an interface{} into a map with the package reflect is with MapRange.
I quote:
MapRange returns a range iterator for a map. It panics if v's Kind is
not Map.
Call Next to advance the iterator, and Key/Value to access each entry.
Next returns false when the iterator is exhausted. MapRange follows
the same iteration semantics as a range statement.
Example:
iter := reflect.ValueOf(m).MapRange()
for iter.Next() {
key := iter.Key().Interface()
value := iter.Value().Interface()
...
}

Asynchronous messages golang

I have a golang server doing something like this:
package main
func main() {
for {
c := listener.Accept()
go handle(c)
}
}
...
func handle(c net.Conn) {
m := readMessage(c) // func(net.Conn)Message
r := processMessage(m) //func(Message)Result
sendResult(c, r) // func(net.Conn,Result)
}
Which reads and writes messages synchronously. What I need now is to send messages asynchronously through a given open connection, I know a channel can be used by I'm kind of lost.
This is my idea:
...
func someWhereElese(c chan Result) {
// generate a message and a result
r := createResultFromSomewhere()
c <- r // send the result through the channel
}
And modify my handle to use that same channel instead
func handle(c net.Conn, rc chan Result) {
m := readMessage(c) // func(net.Conn)Message
r := processMessage(m) //func(Message)Result
//sendResult(c, r) // func(net.Conn,Result)
rc <- r
}
And here's where my confusion lies.
The result channel should be created and it should have a connection where to send whatever it receives
func doSend(c net.Con, rc chan Result) {
r := rc // got a result channel
sendResult(c, r) // send it through the wire
}
But where should that channel be created? In the main loop?
func main() {
...
for {
c := l.Accept()
rc := make(chan Result)
go doSend(c, rc)
}
}
What about the read? Should it go in it's own channel/gorutine?
If I need to broadcast to n clients, should I keep a slice of result channels? a slice of connections?
I'm kind of confused here, but I feel I'm close.
This program seems to solve my immediate question
package main
import (
"bytes"
"encoding/binary"
"log"
"net"
)
var rcs []chan int = make([]chan int,0)
func main() {
a, e := net.ResolveTCPAddr("tcp", ":8082")
if e != nil {
log.Fatal(e)
}
l, e := net.ListenTCP("tcp", a)
for {
c, e := l.Accept()
if e != nil {
log.Fatal(e)
}
rc := make(chan int)
go read(c, rc)
go write(c, rc)
rcs = append(rcs, rc)
// simulate broacast
log.Println(len(rcs))
if len(rcs) > 5 {
func() {
for _, v := range rcs {
log.Println("sending")
select {
case v <- 34:
log.Println("done sending")
default:
log.Println("didn't send")
}
}
}()
}
}
}
func read(c net.Conn, rc chan int) {
h := make([]byte, 2)
for {
_, err := c.Read(h)
if err != nil {
rc <- -1
}
var v int16
binary.Read(bytes.NewReader(h[:2]), binary.BigEndian, &v)
rc <- int(v)
}
}
func write(c net.Conn, rc chan int) {
for {
r := <-rc
o := []byte{byte(r * 2)}
c.Write(o)
}
}

More easy way of using Interfaces to map YAML dynamically?

I am trying to parse a yaml file dynamically (Therefore no struct).
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"log"
)
func main() {
var out = `
a: First!
f: Second
b:
c:
f: Third
`
m := make(map[interface{}]interface{})
err := yaml.Unmarshal([]byte(out), &m)
if err != nil {
log.Fatal(err)
}
fmt.Println(m["b"].(map[interface{}]interface{})["c"].(map[interface{}]interface{})["f"])
}
Everytime I have to access a subkey, I am forced to convert map variable in question to (map[interface{}]interface{}). This is causing bit of a hassle for me as I have to iterate through the map.
Is there any easier method for parsing YAML file in Go?
Another approach is to flatten the yaml data structure into a key,value map in which keys and values are strings. Then if you need the actual type (5 being an int) you can do the conversion yourself. Example:
"a" = "First!"
"f" = "Second"
"b.c.f" = "Third"
"b.c.g.size" = "2"
"b.c.g.0 = "zero"
"b.c.g.1 = "one"
In Go:
func main() {
any := map[string]interface{}{}
err := yaml.Unmarshal([]byte(out), &any)
if err != nil {
log.Fatal(err)
}
flatmap := map[string]string{}
for k, v := range any {
flatten(k, v, flatmap)
}
for k, v := range flatmap {
fmt.Println(k, "=", v)
}
}
func flatten(prefix string, value interface{}, flatmap map[string]string) {
submap, ok := value.(map[interface{}]interface{})
if ok {
for k, v := range submap {
flatten(prefix+"."+k.(string), v, flatmap)
}
return
}
stringlist, ok := value.([]interface{})
if ok {
flatten(fmt.Sprintf("%s.size", prefix), len(stringlist), flatmap)
for i, v := range stringlist {
flatten(fmt.Sprintf("%s.%d", prefix, i), v, flatmap)
}
return
}
flatmap[prefix] = fmt.Sprintf("%v", value)
}

How to cast reflect.Value to its type?

How to cast reflect.Value to its type?
type Cat struct {
Age int
}
cat := reflect.ValueOf(obj)
fmt.Println(cat.Type()) // Cat
fmt.Println(Cat(cat).Age) // doesn't compile
fmt.Println((cat.(Cat)).Age) // same
Thanks!
concreteCat,_ := reflect.ValueOf(cat).Interface().(Cat)
see http://golang.org/doc/articles/laws_of_reflection.html
fox example
type MyInt int
var x MyInt = 7
v := reflect.ValueOf(x)
y := v.Interface().(float64) // y will have type float64.
fmt.Println(y)
Ok, I found it
reflect.Value has a function Interface() that converts it to interface{}
This func auto-converts types as needed. It loads a config file values into a simple struct based on struct name and fields:
import (
"fmt"
toml "github.com/pelletier/go-toml"
"log"
"os"
"reflect"
)
func LoadConfig(configFileName string, configStruct interface{}) {
defer func() {
if r := recover(); r != nil {
fmt.Println("LoadConfig.Recovered: ", r)
}
}()
conf, err := toml.LoadFile(configFileName)
if err == nil {
v := reflect.ValueOf(configStruct)
typeOfS := v.Elem().Type()
sectionName := getTypeName(configStruct)
for i := 0; i < v.Elem().NumField(); i++ {
if v.Elem().Field(i).CanInterface() {
kName := conf.Get(sectionName + "." + typeOfS.Field(i).Name)
kValue := reflect.ValueOf(kName)
if (kValue.IsValid()) {
v.Elem().Field(i).Set(kValue.Convert(typeOfS.Field(i).Type))
}
}
}
} else {
fmt.Println("LoadConfig.Error: " + err.Error())
}
}
Seems the only way would be to do a switch statement similar to (code below) (also, something like the commented line would've-been nice though doesn't work (:()):
func valuesFromStruct (rawV interface{}) []interface{} {
v := reflect.ValueOf(rawV)
out := make([]interface{}, 0)
for i := 0; i < v.NumField(); i += 1 {
field := v.Field(i)
fieldType := field.Type()
// out = append(out, field.Interface().(reflect.PtrTo(fieldType)))
switch (fieldType.Name()) {
case "int64":
out = append(out, field.Interface().(int64))
break`enter code here`
case "float64":
out = append(out, field.Interface().(float64))
break
case "string":
out = append(out, field.Interface().(string))
break
// And all your other types (here) ...
default:
out = append(out, field.Interface())
break
}
}
return out
}
Cheers!

Resources