Say I make a call to an api, which returns a slice of structs, each with a load of methods and fields, but I only want to use one field per each element of the returned values. How can I do this?
For instance, I call out to an API, and it returns a slice of x elements, each which has 4 values and 13method, but I only want 1 value and 0 methods (the slice of fetus structs). How can I marshall this into my own struct? eg:
func GETApi() []fetus {
//doGet() returns a slice of persons, which are described below
a := doGet() // this gets many detailed persons as a slice, but I just want them as a slice of fetus
/*
type person struct {
id:
age:
height:
width:
}
func (a *person) GetHeight() int { ... }
func (a *person) GetWidth() int { ... }
func (a *person) GetLaught() int { ... }
// I want to return a slice of these ([]fetus)
type fetus struct {
id:
}
var f fetus
f := a // how can I condense said slice of persons into a slice of fetus
return f
*/
Perhaps something like this?
package main
type Person struct {
Id string
Name string
Age string
LotsOfOtherFields string
}
type Fetus struct {
Id string
}
func main() {
persons := []Person{
{Id: "a", Name: "John"},
{Id: "b", Name: "Steve"},
{Id: "c", Name: "Fred"},
}
fetuses := make([]Fetus, len(persons))
for i, p := range persons {
// Create a new fetus struct and pluck the ID from the person struct
fetuses[i] = Fetus{Id: p.Id}
}
}
Related
I'm still new to Go and am facing a situation that needs some help here.
Assume there are two types of structs with the SAME attribute list:
type PersonA struct {
[long list of attributes...]
}
type PersonB struct {
[same long list of attributes...]
}
And I would like to create an instance and initialize based on some conditions like below:
var smartPerson [type]
func smartAction(personType string) {
switch personType
case "A":
smartPerson = PersonA{long list initialization}
case "B":
smartPerson = PersonB{same long list initialization}
}
fmt.Println(smartPerson)
There are two problems here:
First - 'smartPerson' needs to be the instance type ascertained in the switch statement.
Second - the 'long list initialization' is the same for all conditions, so better to avoid repetition.
Is it possible to do this in Go?
You can, do something like this by embedding a common struct in both PersonA and PersonB.
For example (playgound link):
package main
import "fmt"
type commonPerson struct {
A string
B string
C string
}
type PersonA struct {
commonPerson
}
func (p PersonA) String() string {
return fmt.Sprintf("A: %s, %s, %s", p.A, p.B, p.C)
}
// This function is just here so that PersonA implements personInterface
func (p PersonA) personMarker() {}
type PersonB struct {
commonPerson
}
func (p PersonB) String() string {
return fmt.Sprintf("B: %s, %s, %s", p.A, p.B, p.C)
}
// This function is just here so that PersonB implements personInterface
func (p PersonB) personMarker() {}
type personInterface interface {
personMarker()
}
var smartPerson personInterface
func smartAction(personType string) {
common := commonPerson{
A: "foo",
B: "bar",
C: "Hello World",
}
switch personType {
case "A":
smartPerson = PersonA{commonPerson: common}
case "B":
smartPerson = PersonB{commonPerson: common}
}
}
func main() {
smartAction("A")
fmt.Println(smartPerson)
smartAction("B")
fmt.Println(smartPerson)
}
Outputs:
A: foo, bar, Hello World
B: foo, bar, Hello World
Need help in understanding how to update a slice that is contained in a struct and passed to a function.
Function addBookToShelfInLibrary(l *library, shelfid int, b book) - takes as input a library, tries to add to book to the shelf with the id = shelfid (passed as param). The function appends to the books array and also assigns it to books array. What am I missing?
At the end of code run, I expect the books to contain two books, "harrypotter", "bible" but I see only one, i.e. harrypotter. Also, I am passing a pointer to the library but I don't think that matters in this case.
playground code:- https://play.golang.org/p/JrjtLSj-jHI
func main() {
lib := library{
shelves: []shelf{
{
id: 1,
books: []book{
{name: "harrypotter"},
},
},
},
}
addBookToShelfInLibrary(&lib, 1, book{name: "bible"})
fmt.Printf("%v", lib)
}
type library struct {
shelves []shelf
}
type shelf struct {
id int
books []book
}
type book struct {
name string
}
func addBookToShelfInLibrary(l *library, shelfid int, b book) {
for _, s := range (*l).shelves {
if s.id == shelfid {
//found shelf, add book
s.books = append(s.books, b)
}
}
}
Thanks for your answer / explanation in advance.
In this statement:
s := range (*l).shelves
the variable s is a copy of the slice element. The later call to append modifies this copy, the not slice element. Change the code to modify the slice element:
func addBookToShelfInLibrary(l *library, shelfid int, b book) {
for i := range l.shelves {
s := &l.shelves[i]
if s.id == shelfid {
//found shelf, add book
s.books = append(s.books, b)
}
}
}
Another approach is to use a pointer to a shelf:
type library struct {
shelves []*shelf
}
lib := library{
shelves: []*shelf{
{
...
All other code remains the same. Run it on the playground.
I have multiple structs like bellow
type Person struct {
first string
last string
age int
}
type Product struct {
ProductID int
Name string
Description string
Price float64
}
I want a function that will take a slice of any type as the first argument and a function as a second argument that it will use to call with each element of the slice to construct a string slice and will return that string slice. Something like map() in Typescript/scala or select() in C#.
Since Go doesn't have generics, "any type" can only mean interface{}. You can have something like this:
func ToStringSlice(arr []interface{}, convert func(interface{}) string) []string {
ret := []string{}
for _, elem := range arr {
ret = append(ret, convert(elem))
}
return ret
}
Then you can basically inject any conversion function you want. e.g.
fmt.Println(ToStringSlice([]interface{}{1, 2, "foo", "bar"},
func(x interface{}) string {
return fmt.Sprint(x)
}))
And since string conversions can go bad, I'd also add error checking:
// Define the function with an error return.
type Stringifier func(interface{}) (string, error)
func ToStringSlice(arr []interface{}, convert Stringifier) ([]string, error) {
ret := []string{}
for _, elem := range arr {
if s, e := convert(elem); e != nil {
return nil, e
} else {
ret = append(ret, s)
}
}
return ret, nil
}
one solution involves fmt.Stringer
package main
import (
"fmt"
)
func main() {
items := []fmt.Stringer{Product{ProductID: 1}, Person{first: "first"}}
var res []string
for _, i := range items {
res = append(res, i.String())
}
fmt.Println(res)
}
type Person struct {
first string
last string
age int
}
func (p Person) String() string { return p.first }
type Product struct {
ProductID int
Name string
Description string
Price float64
}
func (p Product) String() string { return fmt.Sprint(p.ProductID) }
That's a typical use case for an interface. Luckily enough the interface you need already exists as fmt.Stringer (https://golang.org/pkg/fmt/#Stringer):
Have a look at https://play.golang.org/p/d1sNPLKhNCU...
First your structs are required to implement the Stringer interface. This is done by simply adding a menthode String(), for example:
type Product struct {
ProductID int
Name string
Description string
Price float64
}
func (p Product) String() string {
return fmt.Sprintf("%d %s", p.ProductID, p.Name)
}
Now your Product is also a Stringer and can be Therefore be passed in any function that accepts Stringers:
func AsStringSlice(in []fmt.Stringer) []string {
out := []string{}
for _, i := range in {
out = append(out, i.String())
}
return out
}
Since "interface{} says nothing" (https://go-proverbs.github.io/) I would recommend to use interface{} as rarely as possible. This is achieved in this approach. Also you can avoid reflection that way.
On the other hand you have to be able to manage the structs, eg. implement the String function. If thats not possible because the Types come from a dependency consider wrapping them:
type MyTypeWithStringer package.ForeinTypeWithoutStringer
func(t MyTypeWithStringer) String() string {
// ...
}
This is a solution that fits me best
type Person struct {
first string
last string
age int
}
type Product struct {
ProductID int
Name string
Description string
Price float64
}
func mapToStringSlice(slice interface{}, mapFunc func(interface{}) string) []string {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic("mapToStringSlice() given a non-slice type")
}
ret := make([]string, s.Len())
for i:=0; i<s.Len(); i++ {
ret[i] = mapFunc(s.Index(i).Interface())
}
return ret
}
func main() {
persons := []Person{{
first: "A",
last: "g",
age: 20,
},{
first: "B",
last: "r",
age: 40,
},{
first: "C",
last: "",
age: 0,
}}
products := []Product{Product{ProductID: 1}, Product{ProductID: 2}}
personFirstNames := mapToStringSlice(persons, func(input interface{}) string {
return input.(Person).first
})
productIDs := mapToStringSlice(products, func(input interface{}) string {
return input.(Product).ProductID
})
}
Is there any way to create a map into several structs, and then use It?
I have several different structs that implement the same interface and matching input types for each struct.
I want to read data from the different inputs into the structs - without knowing the input type at the compilation time.
type myInput struct {
InputType string
data []bytes
}
// Will get as an input after compeleation
inputs := []myInput{
myInput{InputType: "a", data: []bytes{0x01, 0x02, 0x03}},
myInput{InputType: "b", data: []bytes{0x01, 0x02}},
}
type StructA struct {
A uint16
B uint32
}
func (a StructA) name () {
fmt.Printf("name is: %d %d", a.A, a.B)
}
type StructB struct {
C uint32
}
func (b StructB) name () {
fmt.Printf("name is: %d", b.C)
}
AorB map[string]<???> {
"a": StructA,
"b": StructB,
}
At this point, I don't know what to do. I need to take the correct struct by the input type and initialize the struct using binary.Read.
for _, myInput := range (inputs) {
// ???? :(
myStruct := AtoB[myInput.InputType]{}
reader :=bytes.NewReader(input1)
err := binary.Read(reader, binary.BigEndian, &myStruct)
fmt.Printf(myStruct.name())
}
Thanks!
Define a interface
type Bin interface {
name() string
set([]byte) // maybe returning error
}
You'll be handling Bins only.
type StructA struct { /* your code so far */ }
type StructB struct { /* your code so far */ }
func (a *StructA) set(b []byte) {
a.A = b[0]<<8 + b[1] // get that right, too lazy to code this for you
a.B = b[2]<<24 + b[3]<<16 + ...
}
// same for StructB
So your StructA/B are Bins now.
func makeBin(in myInput) Bin {
var bin Bin
if in.InputType == "a" {
bin = &StructA{}
} else {
bin = &StructB{}
}
bin.set(in.data) // error handling?
return bin
}
If you have more than two types: Use a switch instead if an if or make a tiny type registry (reflect).
First you define an interface for the commonly used func name:
type Namer interface {
name()
}
Then you can create a map to that interface and insert structs:
AorB := map[string] Namer {
"a": StructA{
A: 42,
B: 28,
},
"b": StructB{
C: 12,
},
}
Now you can access all entries:
for _, n := range AorB {
n.name()
}
You can use interface for the same
AorB := map[string]interface{}{
"a": StructA{},
"b": StructB{},
}
When you retrieve value back you can assert into A for type A and B for type B
I would have expected this code to work:
package main
type Item struct {
Key string
Value string
}
type Blah struct {
Values []Item
}
func main() {
var list = [...]Item {
Item {
Key : "Hello1",
Value : "World1",
},
Item {
Key : "Hello1",
Value : "World1",
},
}
_ = Blah {
Values : &list,
}
}
I thought this would be the correct way of doing this; Values is a slice, list is an array. &list should be a slice, which is assignable to Item[], right?
...but instead, it errors with the message:
cannot use &list (type *[2]Item) as type []Item in assignment
In C, you'd write:
struct Item {
char *key;
char *value;
};
struct Blah {
struct Item *values;
};
How do you do that in Go?
I saw this question:
Using a pointer to array
...but either the answers are for a previous version of Go, or they're just plain wrong. :/
A slice is not simply a pointer to an array, it has an internal representation which contains its length and capacity.
If you want to get a slice from list you can do:
_ = Blah {
Values : list[:],
}
Go is, fortunately, not so verbose as it might seem from the OP. This works:
package main
type Item struct {
Key, Value string
}
type Blah struct {
Values []Item
}
func main() {
list := []Item{
{"Hello1", "World1"},
{"Hello2", "World2"},
}
_ = Blah{list[:]}
}
(Also here)
PS: Let me suggest to not write C in Go.
When you are starting out with Go ignore arrays completely and just use slices is my advice. Arrays are rarely used and cause Go beginners a lot of trouble. If you have a slice then you don't need a pointer to it since it is a reference type.
Here is your example with a slice and no pointers which is much more idiomatic.
package main
type Item struct {
Key string
Value string
}
type Blah struct {
Values []Item
}
func main() {
var list = []Item{
Item{
Key: "Hello1",
Value: "World1",
},
Item{
Key: "Hello1",
Value: "World1",
},
}
_ = Blah{
Values: list,
}
}