Trying to implement OOPS in golang with structs - go

I am trying to retain the stats of a struct. What I am trying to do is create a struct using NewGolang and increase the counter, but all the output are 1. I am expecting 1,2,3. Can somebody please explain.
package main
import "fmt"
type Golang struct {
SessionCounter int
}
func NewGolang() *Golang {
return &Golang{
SessionCounter: 0,
}
}
func (g Golang) increaseCounter() {
g.SessionCounter++
fmt.Println(g.SessionCounter)
}
func main() {
obj := NewGolang()
obj.increaseCounter()
obj.increaseCounter()
obj.increaseCounter()
}
Output:
1
1
1
Expected:
1
2
3

When you run method without pointer you copy struct data, when use poiner you change original data.

Change func (g Golang) increaseCounter() to func (g *Golang) increaseCounter(). You need pointer receiver to change the data inside the struct.

Related

Do map of pointers is different with common way of using maps

I want to create cache with map. As map doesn't allow reference to its value, so it's not possible to change values in called functions.
After some search, I found, it's possible with creating map of pointer (of struct). It Almost solve problem and can work like variable by reference
But as i found a few using of this method for map. I worry about using it to be safe.
Is anyone has experience of using map of pointer? and is it right way to use it?
package main
import "fmt"
type Cache struct {
name string
counter int
}
func incr(c Cache) {
c.counter += 1
}
func incrp(c *Cache) {
c.counter += 2
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
incr(c)
fmt.Println(c.name, c.counter) // James 10
mp := make(map[string]*Cache)
mp["james"] = &Cache{name: "James", counter: 10}
cp := mp["james"]
incrp(cp)
fmt.Println(cp.name, cp.counter) // James 12
}
edited: My text had some confusing words and sentences, that caused to misunderstanding, so i tried to fixed it
You can accomplish this and still have a map of non-pointers, with a pointer receiver on the struct:
package main
import "fmt"
type Cache struct {
name string
counter int
}
func (c *Cache) incr() { // the '(c *Cache)' is the receiver;
c.counter += 1 // it makes incr() a method, not just a function
}
func main() {
m := make(map[string]Cache)
m["james"] = Cache{name: "James", counter: 10}
c := m["james"]
c.incr()
fmt.Println(c.name, c.counter)
}
Output:
James 11
If receivers and methods are new to you, here is where they are mentioned in the Tour of Go: https://tour.golang.org/methods/1
Note the page about pointer receivers a few steps later in the Tour: https://tour.golang.org/methods/4

Go - How do you change the value of a pointer parameter?

In Golang, is it possible to change a pointer parameter's value to something else?
For example,
func main() {
i := 1
test(&i)
}
func test(ptr interface{}) {
v := reflect.ValueOf(ptr)
fmt.Println(v.CanSet()) // false
v.SetInt(2) // panic
}
https://play.golang.org/p/3OwGYrb-W-
Is it possible to have test() change i to point to another value 2?
Not sure if this is what you were looking for,
but yes you can change a pointer's value to something else.
The code below will print 2 and 3:
package main
import (
"fmt"
)
func main() {
i := 1
testAsAny(&i)
fmt.Println(i)
testAsInt(&i)
fmt.Println(i)
}
func testAsAny(ptr interface{}) {
*ptr.(*int) = 2
}
func testAsInt(i *int) {
*i = 3
}
Here's now to set the value using the reflect package. The key point is to set the pointer's element, not the pointer itself.
func test(ptr interface{}) {
v := reflect.ValueOf(ptr).Elem()
v.SetInt(2)
}
playground example
Note that the reflect package is not needed for this specific example as shown in another answer.

How to change the pointer of a variable in a struct with method implemented from interface [duplicate]

This question already has answers here:
My object is not updated even if I use the pointer to a type to update it
(3 answers)
X does not implement Y (... method has a pointer receiver)
(4 answers)
Closed 8 months ago.
Edit: For everyone suggesting using a pointer receiver in the function: By changing the method to have a pointer receiver, the structure no longer implements the interface. I have a image at the bottom of my question showing that.
I am trying to make a setter that will mutate the pointer of a variable in a struct with a method implemented from an interface.
package main
import "fmt"
func main() {
i := 1
b := BlahImpl {id:&i}
fmt.Println(b.ID())
j := 2
b.SetID(&j)
fmt.Println(b.ID())
}
type BlahInterface interface {
SetID(*int)
ID() int
}
type BlahImpl struct {
id *int
}
func (b BlahImpl) SetID(i *int) {
b.id = i
}
func (b BlahImpl) ID() int {
return *b.id
}
The current output is:
1
1
But I would like:
1
2
When I use pointer receiver I get this error because the struct is no longer implementing the interface.
Well, to be honest I do not quite get why it works this way, but this works as you want it to:
package main
import "fmt"
func main() {
i := 1
b := BlahImpl{id: &i}
fmt.Println(b.ID())
j := 2
b.SetID(&j)
fmt.Println(b.ID())
}
type BlahInterface interface {
SetID(*int)
ID() int
}
type BlahImpl struct {
id *int
}
func (b *BlahImpl) SetID(i *int) {
b.id = i
}
func (b *BlahImpl) ID() int {
return *b.id
}
The difference is how the structure's methods are defined. I've added * before the name so reference on the structure is passed into the method. It looks like without * the method gets copy of the struct so the modification does not work.

How to change pointer slice in golang

I'm trying to get a better understanding of go. I created a little exercise for myself: pass a pointer slice to a function and modify it.
This is what I came up with:
package main
import (
"fmt"
"unsafe"
)
func main() {
var data *[]byte
fillData(data)
fmt.Println((*data)[0:5])
}
func fillData(data *[]byte) {
b := []byte("hello")
fmt.Println(b[0:5])
fmt.Println(string(b[0:5]))
data = (*[]byte)(unsafe.Pointer(&b[0]))
}
But it gives an invalid memory address or nil pointer dereference error. I know I wouldn't use something like this in real code but I was just curious how to pass a slice and modify it without returning it.
https://play.golang.org/p/_K5ltKKKNV
When you try to update data in fillData, you make two errors. First, you update the pointer rather than what it's pointed to. Second, data is a nil pointer, so writing through that pointer will cause a nil pointer error.
Here's one possible way to write the code. data starts as a zero'ed slice, and gets updated inside fillData. This will copy the slice information (len, cap, and pointer to array) from b to *data which means that data will share information with b (importantly, including sharing the underlying array).
package main
import "fmt"
func main() {
var data []byte
fillData(&data)
fmt.Println(data, data[0:5])
}
func fillData(data *[]byte) {
b := []byte("hello")
*data = b[0:1]
}
Another way would be to have data being a pointer, and updating it. Then you have to pass a double pointer into fillData. That would look like this:
package main
import "fmt"
func main() {
var data *[]byte
fillData(&data)
fmt.Println((*data)[0:5])
}
func fillData(data **[]byte) {
b := []byte("hello")
*data = &b
}
Finally, the best way to write this code isn't to use pointers at all, and just return the slice. Unlike C or C++, it's rarely needed to use "output" parameters to functions. That's because go allows multiple return values.
package main
import "fmt"
func main() {
data := getData()
fmt.Println(data, data[0:5])
}
func getData() []byte {
return []byte("hello")[:1]
}

golang: can i share C.int between packages

in the main package i have:
var foo C.int
foo = 3
t := fastergo.Ctuner_new()
fastergo.Ctuner_register_parameter(t, &foo, 0, 100, 1)
in the fastergo package i have:
func Ctuner_register_parameter(tuner unsafe.Pointer, parameter *C.int, from C.int, to C.int, step C.int) C.int {
...
}
if i try to run it, i get:
demo.go:14[/tmp/go-build742221968/command-line-arguments/_obj/demo.cgo1.go:21]: cannot use &foo (type *_Ctype_int) as type *fastergo._Ctype_int in function argument
i am not really sure what go is trying to tell me here, but somehow i think it wants to tell me, that all C.int are not equal? why is this the case? how can i solve this / work around?
Since _Ctype_int doesn't begin with a Unicode upper case letter, the type is local to the package. Use Go types, except in the C wrapper package where you convert them to C types. The wrapper package should hide all the implementation details.
You don't provide sufficient information for us to create sample code which compiles and runs. Here's a rough outline of what I expected to see:
package main
import "tuner"
func main() {
var foo int
foo = 3
t := tuner.New()
t.RegisterParameter(&foo, 0, 100, 1)
}
.
package tuner
import (
"unsafe"
)
/*
#include "ctuner.h"
*/
import "C"
type Tuner struct {
ctuner uintptr
}
func New() *Tuner {
var t Tuner
t.ctuner = uintptr(unsafe.Pointer(C.ctuner_new()))
return &t
}
func (t *Tuner) RegisterParameter(parameter *int, from, to, step int) error {
var rv C.int
rv = C.ctuner_register_parameter(
(*C.ctuner)(unsafe.Pointer(t.ctuner)),
(*C.int)(unsafe.Pointer(parameter)),
C.int(from),
C.int(to),
C.int(step),
)
if rv != 0 {
// handle error
}
return nil
}
As explained by peterSO, you can't pass C.int between packages. However, you can pass pointers between packages by converting the pointer type. To do this, you would define a named type in the target package, import that type into the calling package and covert via unsafe.Pointer. There isn't any point in doing this with a single int.
However, it is helpful if you keep code to convert complex types in a package; for example an array of strings (or any sort of nested array).
The example below is for exporting a go function to be called in C, but this works in reverse, ie. if you want to call a C functions which a returns nested array.
package convert
import "C"
type PP_char **C.char
func From_c_to_go(arr_str PP_char, length int) []string {
// Some operation on the Ctype
var slice []string
for _, s := range unsafe.Slice(arr_str, length) {
if s == nil {
break
}
x := C.GoString(s)
slice = append(slice, x)
}
return slice
}
package main
import "C"
import "convert"
//export myFunc
func myFunc(arr_str **C.char, length int){
retyped_arr_str := convert.PP_char(unsafe.Pointer(arr_str))
slice := convert.From_c_to_go(retyped_arr_str, length)
// Do something with slice
}
You could instead decide to pass instance of unsafe.Pointer as an argument to the go function in the target package and perform the type conversion in that function.

Resources