logo
down
shadow

Convert []string to []interface{}


Convert []string to []interface{}

By : ChrisA
Date : November 14 2020, 04:51 PM
To fix this issue
now I know I'm wrong, []type is a whole type, can't be considered as []interface{}.
code :
t := []int{1, 2, 3, 4}
s := make([]interface{}, len(t))
for i, v := range t {
    s[i] = v
}


Share : facebook icon twitter icon
Is there a way to convert COM interface GUIDs to the string name of the interface?

Is there a way to convert COM interface GUIDs to the string name of the interface?


By : Jesse Feng
Date : March 29 2020, 07:55 AM
seems to work fine You could write one. Just look up your GUID in the registry under HKEY_CLASSES_ROOT\Interface
How could I convert an []interface{} into a []string in Go?

How could I convert an []interface{} into a []string in Go?


By : Spencer Debenport
Date : March 29 2020, 07:55 AM
seems to work fine You need to construct a new array of type []string in order to use strings.Join:
code :
import "fmt"
import "strings"

func main() {
    s1 := []interface{}{"a", "b", "c"}
    s2 := make([]string, len(s1))
    for i, s := range s1 {
        s2[i] = s.(string)
    }
    fmt.Println(strings.Join(s2, ", "))
}
Convert map[interface {}]interface {} to map[string]string

Convert map[interface {}]interface {} to map[string]string


By : user2749181
Date : March 29 2020, 07:55 AM
Does that help A secure way to process unknown interfaces, just use fmt.Sprintf()
https://play.golang.org/p/gOiyD4KpQGz
code :
package main

import (
    "fmt"
)

func main() {

    mapInterface := make(map[interface{}]interface{})   
    mapString := make(map[string]string)

    mapInterface["k1"] = 1
    mapInterface[3] = "hello"
    mapInterface["world"] = 1.05

    for key, value := range mapInterface {
        strKey := fmt.Sprintf("%v", key)
        strValue := fmt.Sprintf("%v", value)

        mapString[strKey] = strValue
    }

    fmt.Printf("%#v", mapString)
}
Convert from class to Interface with a generic type interface Interface<Interface>

Convert from class to Interface with a generic type interface Interface<Interface>


By : jorellis
Date : March 29 2020, 07:55 AM
it fixes the issue In order to make it work, you need to make your generic interfaces covariant. But since ICollection is not covariant, it would require also changing the Children type to IReadOnlyCollection and remove the setters if you can afford that.
code :
public interface ITaxonomyHasChildren<out TChild> : ITaxonomy where TChild : ITaxonomy
{
    IReadOnlyCollection<TChild> Children { get; }
}

public interface ITaxonomyHasParent<out TParent> : ITaxonomy where TParent : ITaxonomy
{
    TParent Parent { get; }
}
private void SelectedEntityChanged(ITaxonomy selectedEntity)
{
    CommonName = selectedEntity.CommonName;
    ScientificName = selectedEntity.ScientificName;

    Children = (selectedEntity as ITaxonomyHasChildren<ITaxonomy>)?.Children;
    Parent = (selectedEntity as ITaxonomyHasParent<ITaxonomy>)?.Parent;
}
class Taxonomy : ITaxonomy, ITaxonomyHasChildren<Taxonomy>, ITaxonomyHasParent<Taxonomy>
{
    public string CommonName { get; set; }
    public string ScientificName { get; set; }
    public Taxonomy Parent { get; set; }
    public List<Taxonomy> Children { get; set; } = new List<Taxonomy>();
    IReadOnlyCollection<Taxonomy> ITaxonomyHasChildren<Taxonomy>.Children => Children;
}
How to convert different values from map[string]interface{} to type string in golang?

How to convert different values from map[string]interface{} to type string in golang?


By : Manuel Brihuega
Date : March 29 2020, 07:55 AM
Any of those help @Adrian and @Kaedys comments point to the correct answer. Developing it a bit more you could do something as:
code :
package main

import "fmt"

func main() {
    map1 := map[string]interface{}{"str1": "string one", "int1": 123, "float1": 0.123}

    var slc []string
    for _, j := range map1 {
        switch v := j.(type) {
        case string:
            slc = append(slc, v)
        case fmt.Stringer:
            slc = append(slc, v.String())
        default:
            slc = append(slc, fmt.Sprintf("%v", v))
        }
    }

    fmt.Println(slc)
}
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org