6

This questions follows another question of mine.

I don't exactly get what is wrong with my attempt to convert res to a ListSociete in the following test code :

import (
    "errors"
    "fmt"
    "github.com/jmcvetta/neoism"
)

type Societe struct {
    Name string
}

type ListSociete []Societe

func loadListSociete(name string) (ListSociete, error) {
    db, err := neoism.Connect("http://localhost:7474/db/data")
    if err != nil {
        return nil, err
    }
    res := []struct {
        Name string `json:"a.name"`
    }{}
    cq := neoism.CypherQuery{
        Statement: `
            MATCH (a:Societe)
            WHERE a.name = {name}
            RETURN a.name
            `,
        Parameters: neoism.Props{"name": name},
        Result:     &res,
    }
    db.Cypher(&cq)
    if len(res) == 0 {
        return nil, errors.New("Page duz not exists")
    }
    r := res[0]
    return ListSociete(res), nil
}

Is a []struct{Name string} different from a []struct{Name string json:"a.name" } ?

Or is a ListSociete different from a []struct{Name string} ?

Thanks.

Community
  • 1
  • 1
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54

2 Answers2

11

You are currently dealing with two different types:

type Societe struct {
    Name string
}

and the anonymous one:

struct {
    Name string `json:"a.name"`
}

These two would be identical if it wasn't for the tag. The Go Specifications states (my emphasis):

Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags. Two anonymous fields are considered to have the same name. Lower-case field names from different packages are always different.

So, you can't do a simple conversion between the two. Also, the fact that you are converting slices of the two types makes the conversion problematic. I can see two options for you:

Copy through iteration:

This is the safe and recommended solution, but it is also more verbose and slow.

ls := make(ListSociete, len(res))
for i := 0; i < len(res); i++ { 
    ls[i].Name = res[i].Name
}
return ls, nil

Unsafe conversion:

Since both types have the same underlying data structure, it is possible to do an unsafe conversion.
This might however blow up in your face later on. Be warned!

return *(*ListSociete)(unsafe.Pointer(&res)), nil

Playground Example: http://play.golang.org/p/lfk7qBp2Gb

ANisus
  • 74,460
  • 29
  • 162
  • 158
  • While you can convert between structs that differ only in tags, you cannot convert between slices of such structs. Why? See https://play.golang.org/p/RSRoPnvXXU7 – Peter Dotchev Apr 03 '18 at 19:29
  • This seems like a language design flaw. You need either to copy the slice content - a major performance hit or use unsafe memory pointers. Both of these are against the guiding principles of Go. – Peter Dotchev Apr 04 '18 at 05:09
  • "You need either to copy the slice content" hmm.. what about empty slice? If needed copy an empty slice of structs to empty slice of interfaces – Roman Golubin Sep 19 '19 at 10:08
  • This answer was correct at the time, but as of Go 1.8 (Feb 2017) this restriction has been removed so that two structs that only differ in their tags can now be converted from one type to the other, allowing this kind of assignment. The conversion still has to be explicit, however: they are still treated as different types. See https://beta.golang.org/doc/go1.8#language – JVMATL Nov 05 '19 at 15:05
  • (To expand on my previous comment: you still cannot assign a whole slice of differing struct types at once, but you can now assign entire 'compatible' structs at once, instead of having to copy individual elements of each struct) – JVMATL Nov 05 '19 at 15:11
1

So, after some tests, here's whats i found out :

A ListSociete defined as such...

type Societe struct {
    Name string `json:"a.name"`
}

type ListSociete []Societe

is different from this :

type ListSociete []struct {
    Name string `json:"a.name"`
}

This second solution works, whereas the first doesn't.

So I assume there really is no way to convert (directly without writing an explicit loop) between types with different tags ?

In that case, i'll definitely go with the loop, as using tags directly in types (cf. second solution above) would make my code unreadable and unreusable, also I really have no clue what I would be messing with using the unsafe conversion method. So thanks for confirming different tags made different types.

Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
  • Loop whiwh works just fine, by the way. Is it really that much slower ? (to execute, i mean) – Nicolas Marshall Jul 09 '14 at 12:29
  • It depends on the size of the list. The unsafe conversion is an O(1) operation while the loop is O(N). Most likely, the iteration is fast enough for most uses. Strange that it didn't work for you. It works fine in the **[playground example](http://play.golang.org/p/ODqmpfvChd)** – ANisus Jul 09 '14 at 22:31
  • I must have done a mistake somewhere, then. I'll try to make this work again. – Nicolas Marshall Jul 10 '14 at 13:48