Modifying a struct slice within a struct in Go - pointers

In the following example, a person has a slice of friendships, and I try to initialize a friendship as a pointer to another person object, but for some reason it fails, and the result is that nobody has any friendships. Am I not using a pointer somewhere where I should be?
package main
import (
"fmt"
"math/rand"
)
type friendship struct {
friend *person
}
type person struct {
name int
friendship []friendship
}
func createPerson(id int) person {
return person{id, make([]friendship, 0)}
}
func (p *person) addFriends(possibleFriends []*person, numFriends int) {
var friend *person
for i := 0; i < numFriends; i++ {
friend = possibleFriends[rand.Intn(len(possibleFriends))]
p.friendship = append(p.friendship, friendship{friend})
}
}
func main() {
numPeople := 20
people := make([]person, numPeople)
possibleFriends := make([]*person, numPeople)
for i := 0; i < numPeople; i++ {
people[i] = createPerson(i)
possibleFriends[i] = &(people[i])
}
for _, p := range people {
p.addFriends(possibleFriends, 2)
}
fmt.Println(people)
}

use
for i := 0; i < numPeople; i++ {
people[i].addFriends(possibleFriends, 2)
}
or
for i, _ := range people {
people[i].addFriends(possibleFriends, 2)
}
instead of
for _, p := range people {
p.addFriends(possibleFriends, 2)
}
this is because p is a copy of people[i], addFriends has no effect on slice people

Related

trouble using pointer in Golang

I am new to golang, here I am using BubleSort and InsertionSort and generating a rando slice for the functions. Can I use pointers some how to hand both functions the unsorted slice? because when I run the program the first function sorts the slice and the the second function uses that sorted slice. I know there are different ways to give both functions the unsorted slice, but I want to see how can I use pointers to do this. Thank you.
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
slice := generateSlice(4)
fmt.Println(BubleSort(slice))
fmt.Println(InsertionSort(slice))
}
func generateSlice(size int) []int {
slice := make([]int, size, size)
rand.Seed(time.Now().UnixNano())
for i := 0; i < size; i++ {
slice[i] = rand.Intn(10)
}
return slice
}
func BubleSort(slice []int) []int {
fmt.Println("unsorted Buble", slice)
for i := 1; i <= len(slice); {
for j := 0; j < len(slice)-i; {
if slice[j] > slice[j+1] {
slice[j], slice[j+1] = slice[j+1], slice[j]
}
j++
}
i++
}
return slice
}
func InsertionSort(slice []int) []int {
fmt.Println("unsorted Insertion", slice)
for i := 1; i <= len(slice)-1; {
// Check j and j-1 and swap the smaller number to left in each
itteartion to reach the first 2 elements of the slice
for j := i; j >= 1; {
if slice[j] < slice[j-1] {
slice[j], slice[j-1] = slice[j-1], slice[j]
}
j--
}
i++
}
return slice
}
Andy is correct
As an alternative, you can copy the slice before sorting it, as in here (on Play):
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
slice := generateSlice(4)
fmt.Println(BubleSort(copySlice(slice)))
fmt.Println(InsertionSort(copySlice(slice)))
}
func copySlice(src []int) []int {
dest := make([]int, len(src))
copy(dest, src)
return dest
}
func generateSlice(size int) []int {
slice := make([]int, size, size)
rand.Seed(time.Now().UnixNano())
for i := 0; i < size; i++ {
slice[i] = rand.Intn(10)
}
return slice
}
func BubleSort(slice []int) []int {
fmt.Println("unsorted Buble", slice)
for i := 1; i <= len(slice); {
for j := 0; j < len(slice)-i; {
if slice[j] > slice[j+1] {
slice[j], slice[j+1] = slice[j+1], slice[j]
}
j++
}
i++
}
return slice
}
func InsertionSort(slice []int) []int {
fmt.Println("unsorted Insertion", slice)
for i := 1; i <= len(slice)-1; {
// Check j and j-1 and swap the smaller number to left in each itteartion to reach the first 2 elements of the slice
for j := i; j >= 1; {
if slice[j] < slice[j-1] {
slice[j], slice[j-1] = slice[j-1], slice[j]
}
j--
}
i++
}
return slice
}
I am learning Go as well. I think the fact that you are using slice is what causing the unwanted behaviour (when I run the program the first function sorts the slice and the the second function uses that sorted slice). As explained here:
A slice does not store any data, it just describes a section of an underlying array.
Changing the elements of a slice modifies the corresponding elements of its underlying array.
Other slices that share the same underlying array will see those changes.
I don't think passing pointer will produce different results as slices point to the same array. What you can do is maybe receive an array instead of slice?
Hope it helps! :)

How to store recursively obtained combinations in a slice in GO?

Combinations can be printed using the following recursive code (inspired from Rosetta)
I thought it would be easy to store the intermediate results in an []int or the set of combination in an [][]int. But, because the function is recursive, it is not so easy than replacing the
fmt.Println(s)
by a
return s
with a minor modification of the function output for example. I also tried to feed a pointer like
p *[][]int
with the variable "s" inside the recursive function, but I failed :-/
I think it is a general problem with recursive functions so if you have some advises to solve this problem it will help me a lot !
Many thanks in advance ! ;)
package main
import (
"fmt"
)
func main() {
comb(5, 3)
}
func comb(n, m int) {
s := make([]int, m)
last := m - 1
var rc func(int, int)
rc = func(i, next int) {
for j := next; j < n; j++ {
s[i] = j
if i == last {
fmt.Println(s)
} else {
rc(i+1, j+1)
}
}
return
}
rc(0, 0)
}
Seems to me that s is being reused by each rc call so you just need to ensure that when storing s into an [][]int you store its copy, so as to not overwrite its contents during the next iteration.
To copy a slice you can use append like this:
scopy := append([]int{}, s...)
https://play.golang.org/p/lggy5JFL0Z
package main
import (
"fmt"
)
func main() {
out := comb(5, 3)
fmt.Println(out)
}
func comb(n, m int) (out [][]int) {
s := make([]int, m)
last := m - 1
var rc func(int, int)
rc = func(i, next int) {
for j := next; j < n; j++ {
s[i] = j
if i == last {
out = append(out, append([]int{}, s...))
} else {
rc(i+1, j+1)
}
}
return
}
rc(0, 0)
return out
}

Reflection type does not have method

I'm trying to use reflection in Go. Why doesn't this code list the methods? It lists the fields.
is this the problem? value interface{} I'm not sure how to pass a generic struct/class/type to a function. Normally I would just pass an Object.
(I'm totally new at this. I'm a C# programmer)
package main
import (
"fmt"
"reflect"
)
func main() {
var B TestType = TestType{TestString: "Hello", TestNumber: 3}
ListMethods(B)
}
func ListMethods(value interface{}) {
fooType := reflect.TypeOf(value)
for i := 0; i < fooType.NumMethod(); i++ {
method := fooType.Method(i)
fmt.Println("Method = " + method.Name)
}
for i := 0; i < fooType.NumField(); i++ {
field := fooType.Field(i)
fmt.Println("Field = " + field.Name)
fmt.Println(reflect.ValueOf(value).Field(i))
}
}
type TestType struct {
TestString string
TestNumber int
}
func (this *TestType) TestFunction() {
fmt.Println("Test")
}
Because you are passing a type and you declared a method to the pointer of the type.
https://play.golang.org/p/B0NdVyxGxt
Look at this example extended from yours:
package main
import (
"fmt"
"reflect"
)
func main() {
var B TestType = TestType{TestString: "Hello", TestNumber: 3}
ListMethods(B)
}
func ListMethods(value interface{}) {
fooType := reflect.TypeOf(value)
ptrFooType := reflect.PtrTo(fooType)
for i := 0; i < fooType.NumMethod(); i++ {
method := fooType.Method(i)
fmt.Println("Method = " + method.Name)
}
for i := 0; i < ptrFooType.NumMethod(); i++ {
method := ptrFooType.Method(i)
fmt.Println("* Method = " + method.Name)
}
for i := 0; i < fooType.NumField(); i++ {
field := fooType.Field(i)
fmt.Println("Field = " + field.Name)
fmt.Println(reflect.ValueOf(value).Field(i))
}
}
type TestType struct {
TestString string
TestNumber int
}
func (this *TestType) TestFunctionPtr() {
fmt.Println("Test")
}
func (this TestType) TestFunction() {
fmt.Println("Test Non Ptr")
}
Notice how the *Type can access also the Type Methods. But Type cannot access to *Type Methods.
To convert from Type to *Type I have used the reflect.PtrTo(Type).

Go: How can I "unpack" a struct?

I have a struct:
type mystruct struct {
Foo string
Bar int
}
I'd like to create SQL insert statements from the struct which have the following form:
m := mystruct{ "Hello" , 1 }
query := "INSERT INTO mytbl ( foo, bar ) VALUES ( ?,? )"
res,err := db.Exec(query, m.Foo, m.Bar)
Now my question is: how can I make the last line dynamically from the struct (or m) itself? I am able to get the struct names using reflect, but I don't know how to create the []interface{} slice for the db.Exec() call. This is what I have tried: (http://play.golang.org/p/GR1Bb61NFH)
package main
import (
"fmt"
"reflect"
)
type mystruct struct {
Foo string
Bar int
}
func main() {
m := mystruct{"Foo", 1}
fmt.Println(readNames(m))
x := unpackStruct(m)
fmt.Printf("%#v\n", x)
}
func unpackStruct(a interface{}) []interface{} {
// "convert" a to m t
// doesn't work, from 'laws of reflection'
s := reflect.ValueOf(&t).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
fmt.Printf("%d: %s %s = %v\n", i,
typeOfT.Field(i).Name, f.Type(), f.Interface())
}
// this is in principle what I want:
m := mystruct{"Hello", 2}
var ret []interface{}
ret = make([]interface{}, s.NumField())
ret[0] = m.Foo
ret[1] = m.Bar
return ret
}
// works fine:
func readNames(a interface{}) []string {
s := reflect.TypeOf(a)
lenStruct := s.NumField()
ret := make([]string, lenStruct)
for i := 0; i < lenStruct; i++ {
ret[i] = s.Field(i).Name
}
return ret
}
If getting the values of the fields is your issue, this code snippet should help:
s := reflect.ValueOf(a)
ret := make([]interface{}, s.NumField())
for i := 0; i < s.NumField(); i++ {
ret[i] = s.Field(i).Interface()
}
If creating the []interface{} value is your problem, using reflect's slice creation mechanisms should work nicely:
slc := reflect.MakeSlice(InterfaceType, len, cap) // See the link below for creating InterfaceType
slc.Index(0).Set(TargetValue)
return slc.Interface()
(Here's the above-mentioned link).
Modifying the above code to loop over the values in the struct instead of just the 0th index shouldn't be too bad.

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