calculating fibonacci concurrently without channels, but pointers are not working - pointers

in this exercise I am trying to compute Fibonacci numbers but by spawning goroutines and sending back result by pointer in argument but I am doing something wrong please help
package main
import (
"fmt"
)
func fib(n int, p *int) {
fmt.Println(n)
var a, b int
if n > 1 {
go fib(n-1, &a)
go fib(n-2, &b)
*p = a + b
} else {
*p = n
}
fmt.Println(*p)
}
func main() {
c := -1
go fib(5, &c)
fmt.Println(c)
}

Thank you #poWar below is my solution posted only to not leave question unanswered (probably not best but gets exercise done)
package main
import (
"fmt"
"sync"
)
func fib(n int, p *int, wg1 *sync.WaitGroup) {
// fmt.Println(n)
a, b := -1, -1
var wg sync.WaitGroup
if n > 1 {
wg.Add(1)
go fib(n-1, &a, &wg)
wg.Add(1)
go fib(n-2, &b, &wg)
wg.Wait()
*p = a + b
} else {
*p = n
}
wg1.Done()
// fmt.Println(*p)
}
func main() {
c := -1
var wg sync.WaitGroup
wg.Add(1)
go fib(30, &c, &wg)
wg.Wait()
fmt.Println(c)
}

Related

own len function with recursion in Go

I'm trying to build simple function to count elements in slice (like len) It must be simple (without additional libs) and with recursion. The problem is when i try to check is slice is empty (is nul).
package main
import "fmt"
func main() {
x := []int{1, 2, 3}
fmt.Println(len2(x))
}
func len2(s []int) int {
if s == nil {
return 0
}
return 1 + len2(s[1:])
}
the result it should be in this example '3'.
It's broken in if s == nil:
panic: runtime error: slice bounds out of range
It panics because you have no valid termination condition.
When your len2() is called with a non-nil empty slice, it attempts to slice it like s[1:], which will be a runtime panic.
Instead of checking for nil slice, check if the slice is empty by comparing its length to 0:
func len2(s []int) int {
if len(s) == 0 {
return 0
}
return 1 + len2(s[1:])
}
Try it on the Go Playground.
If you can't use the builtin len() function (which you already did in your solution), you may use the for ... range:
func len2(s []int) int {
size := 0
for i := range s {
size = i + 1
}
return size
}
Try this on the Go Playground.
And if it must be recursive, then for example:
func len2(s []int) int {
size := 0
for range s {
size = 1 + len2(s[1:])
break
}
return size
}
Try this on the Go Playground.
But know that these are awful solutions compared to using the builtin len().
The following is not a solution with better performance than len but an implementation that does not use any extra libraries and depends on recursion to find length
func len2(s []int) (count int) {
defer func() {
if r := recover(); r != nil {
count = 0
}
}()
return 1 + len2(s[1:])
}
Here is sample code
package main
import "fmt"
func main() {
var x []int = nil
var x1 = []int{1, 2, 3, 4}
var x2 = []int{}
var x3 = make([]int, 10, 20)
fmt.Println(len2(x))
fmt.Println(len2(x1))
fmt.Println(len2(x2))
fmt.Println(len2(x3))
}
func len2(s []int) (count int) {
defer func() {
if r := recover(); r != nil {
count = 0
}
}()
return 1 + len2(s[1:])
}
Checkout the same in playground
If you can leave without recursion here is a function that does not use len() and should be faster then re-slicing recursively.
func len2(s []int) (count int) {
for i := range s {
count = i + 1
}
}
If you do not want to use len() func, you can use cap()
func main() {
x := []int{1, 2, 3}
fmt.Println(len2(x))
}
func len2(s []int) int {
if cap(s) == 0 {
return 0
}
return 1 + len2(s[1:])
}
Try it again
Original Answer:
In order to check if an array (slice) is empty, you should use the function len()
if len(s) == 0 {
return 0
}
Try it

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
}

golang how to print struct value with pointer

package main
import "fmt"
type A struct {
a int32
B *B
}
type B struct {
b int32
}
func main() {
a := &A{
a: 1,
B: &B{
b: 2,
},
}
fmt.Printf("v ==== %+v \n", a)
}
//ret: v ==== &{a:1 B:0xc42000e204}
//??? how to print B's content but not pointer
Basically, you have to do it yourself. There are two ways to do this. Either just print the thing how you want, or implement the Stringer interface for the struct by adding a func String() string, which gets called when you use the format %v. You could also reference each value in the format which is a struct.
Implementing the Stringer interface is the surest way to always get what you want. And you only have to do it once per struct, instead of per format string when you print.
https://play.golang.org/p/PKLcPFCqOe
package main
import "fmt"
type A struct {
a int32
B *B
}
type B struct{ b int32 }
func (aa *A) String() string {
return fmt.Sprintf("A{a:%d, B:%v}",aa.a,aa.B)
}
func (bb *B) String() string {
return fmt.Sprintf("B{b:%d}",bb.b)
}
func main() {
a := &A{a: 1, B: &B{b: 2}}
// using the Stringer interface
fmt.Printf("v ==== %v \n", a)
// or just print it yourself however you want.
fmt.Printf("v ==== A{a:%d, B:B{b:%d}}\n", a.a, a.B.b)
// or just reference the values in the struct that are structs themselves
// but this can get really deep
fmt.Printf("v ==== A{a:%d, B:%v}", a.a, a.B)
}
When you get into larger structs, it becomes a pain to write a bunch of custom String functions. Goconvey currently uses the following project to show diffs and expected/actual output on structs of any size: https://github.com/luci/go-render/blob/master/render/render.go#L51. It includes displaying pointer values.
If you need the output to be re-usable as code (like fmt.Printf("%#v", a) but include pointer values), I have a forked version of the above project which will render full nested pointers as re-usable code:
package main
import (
"fmt"
"github.com/gdexlab/go-render/render"
)
type A struct {
a int32
B *B
}
type B struct {
b int32
}
func main() {
a := &A{
a: 1,
B: &B{
b: 2,
},
}
output := render.AsCode(a)
fmt.Println(output)
}
// outputs: "&A{a:1, B:&B{b:2}}" compared to initial version of "&{a:1 B:0xc42000e204}"
Go Playground Example:
https://play.golang.org/p/tcfJYb0NnVf
Another simple solution is to print the struct using marshaling. This works only for exported (public) variables by capitalizing the first char inside the struct.
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"encoding/json"
)
type A struct {
Aa int32
B *B
}
type B struct {
Bb int32
}
func main() {
a := &A{
Aa: 1,
B: &B{
Bb: 2,
},
}
aJSON, _ := json.Marshal(a)
fmt.Printf("JSON Print - \n%s\n", string(aJSON))
aYAML, _ := yaml.Marshal(a)
fmt.Printf("YAML Print - \n%s\n", string(aYAML))
}
Output :-
JSON Print -
{"Aa":1,"B":{"Bb":2}}
YAML Print -
aa: 1
b:
bb: 2
If you are printing the struct multiple times then implement Stringer interface as follows :-
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type A struct {
Aa int32
B *B
}
func (a A) String() string {
bytes, _ := yaml.Marshal(a)
return string(bytes)
}
type B struct {
Bb int32
}
func main() {
a := &A{
Aa: 1,
B: &B{
Bb: 2,
},
}
fmt.Printf("YAML Print - \n%+v\n", a)
}
Output -
YAML Print -
aa: 1
b:
bb: 2
Use fmt and reflect.
package main
import (
"fmt"
"reflect"
)
type A struct {
a int32
B *B
}
type B struct {
b int32
}
func main() {
a := &A{
a: 1,
B: &B{
b: 2,
},
}
fmt.Printf("%s\n", GetGoString(a)) // output: &A{a: 1, B: &B{b: 2}}
}
func GetGoString(v interface{}) string {
return getGoString(reflect.ValueOf(v))
}
func getGoString(v reflect.Value) string {
switch v.Kind() {
case reflect.Invalid:
return "nil"
case reflect.Struct:
t := v.Type()
out := getTypeString(t) + "{"
for i := 0; i < v.NumField(); i++ {
if i > 0 {
out += ", "
}
fieldValue := v.Field(i)
field := t.Field(i)
out += fmt.Sprintf("%s: %s", field.Name, getGoString(fieldValue))
}
out += "}"
return out
case reflect.Interface, reflect.Ptr:
if v.IsZero() {
return fmt.Sprintf("(%s)(nil)", getTypeString(v.Type()))
}
return "&" + getGoString(v.Elem())
case reflect.Slice:
out := getTypeString(v.Type())
if v.IsZero() {
out += "(nil)"
} else {
out += "{"
for i := 0; i < v.Len(); i++ {
if i > 0 {
out += ", "
}
out += getGoString(v.Index(i))
}
out += "}"
}
return out
default:
return fmt.Sprintf("%#v", v)
}
}
func getTypeString(t reflect.Type) string {
if t.PkgPath() == "main" {
return t.Name()
}
return t.String()
}
Implement Stringer interface to custom type
no external packages
no need to wrap types in other types
example:
package main
import "fmt"
type A struct {
B *B `json:"b"`
}
type B int
func (b *B) String() string {
if b == nil {
return "nil"
}
return fmt.Sprintf("%d", *b)
}
func main() {
var a A
fmt.Printf("a: %+v\n", a)
a.B = B(3)
fmt.Printf("a: %+v\n", a)
}
output:
a: {B:nil}
a: {B:3}

Asynchronous messages golang

I have a golang server doing something like this:
package main
func main() {
for {
c := listener.Accept()
go handle(c)
}
}
...
func handle(c net.Conn) {
m := readMessage(c) // func(net.Conn)Message
r := processMessage(m) //func(Message)Result
sendResult(c, r) // func(net.Conn,Result)
}
Which reads and writes messages synchronously. What I need now is to send messages asynchronously through a given open connection, I know a channel can be used by I'm kind of lost.
This is my idea:
...
func someWhereElese(c chan Result) {
// generate a message and a result
r := createResultFromSomewhere()
c <- r // send the result through the channel
}
And modify my handle to use that same channel instead
func handle(c net.Conn, rc chan Result) {
m := readMessage(c) // func(net.Conn)Message
r := processMessage(m) //func(Message)Result
//sendResult(c, r) // func(net.Conn,Result)
rc <- r
}
And here's where my confusion lies.
The result channel should be created and it should have a connection where to send whatever it receives
func doSend(c net.Con, rc chan Result) {
r := rc // got a result channel
sendResult(c, r) // send it through the wire
}
But where should that channel be created? In the main loop?
func main() {
...
for {
c := l.Accept()
rc := make(chan Result)
go doSend(c, rc)
}
}
What about the read? Should it go in it's own channel/gorutine?
If I need to broadcast to n clients, should I keep a slice of result channels? a slice of connections?
I'm kind of confused here, but I feel I'm close.
This program seems to solve my immediate question
package main
import (
"bytes"
"encoding/binary"
"log"
"net"
)
var rcs []chan int = make([]chan int,0)
func main() {
a, e := net.ResolveTCPAddr("tcp", ":8082")
if e != nil {
log.Fatal(e)
}
l, e := net.ListenTCP("tcp", a)
for {
c, e := l.Accept()
if e != nil {
log.Fatal(e)
}
rc := make(chan int)
go read(c, rc)
go write(c, rc)
rcs = append(rcs, rc)
// simulate broacast
log.Println(len(rcs))
if len(rcs) > 5 {
func() {
for _, v := range rcs {
log.Println("sending")
select {
case v <- 34:
log.Println("done sending")
default:
log.Println("didn't send")
}
}
}()
}
}
}
func read(c net.Conn, rc chan int) {
h := make([]byte, 2)
for {
_, err := c.Read(h)
if err != nil {
rc <- -1
}
var v int16
binary.Read(bytes.NewReader(h[:2]), binary.BigEndian, &v)
rc <- int(v)
}
}
func write(c net.Conn, rc chan int) {
for {
r := <-rc
o := []byte{byte(r * 2)}
c.Write(o)
}
}

How can go-lang curry?

In functional programming likes Haskell, I can define function
add a b = a+b
Then add 3 will return a function that take one parameter and will return 3 + something
How can I do this in GO?
When I define a function that take more than one (say n) parameters, can I only give it one parameter and get another function that take n-1 parameters?
Update:
Sorry for the imprecise words in my original question.
I think my question should be asked as two qeustions:
Is there partial application in GO?
How GO do function curry?
Thanks TheOnly92 and Alex for solving my second question. However, I am also curious about the first question.
To extend on the previous answer, which allows you to take an arbitrary number of arguments:
package main
import (
"fmt"
)
func mkAdd(a int) func(...int) int {
return func(b... int) int {
for _, i := range b {
a += i
}
return a
}
}
func main() {
add2 := mkAdd(2)
add3 := mkAdd(3)
fmt.Println(add2(5,3), add3(6))
}
Perhaps something like
package main
import (
"fmt"
)
func mkAdd(a int) func(int) int {
return func(b int) int {
return a + b
}
}
func main() {
add2 := mkAdd(2)
add3 := mkAdd(3)
fmt.Println(add2(5), add3(6))
}
You can take it a step further by defining a function type and then adding a method to it.
package main
import "fmt"
type Add func(int, int) int
func (f Add) Apply(i int) func(int) int {
return func(j int) int {
return f(i, j)
}
}
func main() {
var add Add = func(i, j int) int { return i + j }
add3 := add.Apply(3)
fmt.Println("add 3 to 2:", add3(2))
}
You can even try with variadic functions:
package main
import "fmt"
type Multiply func(...int) int
func (f Multiply) Apply(i int) func(...int) int {
return func(values ...int) int {
values = append([]int{i}, values...)
return f(values...)
}
}
func main() {
var multiply Multiply = func(values ...int) int {
var total int = 1
for _, value := range values {
total *= value
}
return total
}
var times2 Multiply = multiply.Apply(2)
fmt.Println("times 2:", times2(3, 4), "(expect 24)")
// ... and you can even cascade (if assigned the Multiply type)
times6 := times2.Apply(3)
fmt.Println("times 6:", times6(2, 3, 5, 10), "(expect 1800)")
}
Hope this helps!

Resources