worker pool concept program error please resolve my problem

94 views
Skip to first unread message

yeshwanthchowdary jampala

unread,
Jan 28, 2023, 11:05:30 AM1/28/23
to golang-nuts
Hi everyone,

I'm getting this error:
1)cannot use j (variable of type int) as type string in send
2)cannot use x (variable of type int) as type float32 in argument to comppricevalue
3)cannot use k (variable of type int) as type float32 in send

Code:
package main

import (
    "fmt"
    "time"
)

type ClientOrder2 struct {
    Order_id        int
    Account_id      string
    Currency_symbol string
    Pricevalue      float32
    SellorBuystatus string
}

var orders2 = []ClientOrder2{
    ClientOrder2{
        Order_id:        1,
        Account_id:      "yesh123",
        Currency_symbol: "USD/EUR",
        Pricevalue:      70.001,
        SellorBuystatus: "sell",
    },
    ClientOrder2{
        Currency_symbol: "USD/INR",
        Pricevalue:      70.002,
    },
    ClientOrder2{
        Currency_symbol: "USD/EUR",
        Pricevalue:      71.003,
    },
    ClientOrder2{
        Currency_symbol: "USD/EUR",
        Pricevalue:      70.001,
    },
}

func compcurrency(id string, jobs <-chan string, results chan<- string) <-chan string {
    put := make(chan string, 4)
    go func() {
        for j := range jobs {
            for i := 0; i < 4; i++ {
                put <- j
            }
            fmt.Println("comparing pricevalues", id, "Market open:", j)
            time.Sleep(time.Second)
            results <- j
        }
    }()
    return put
}

func comppricevalue(id float32, jobs1 <-chan float32, results1 chan<- float32) <-chan float32 {
    putt := make(chan float32, 4)
    go func() {
        for k := range jobs1 {
            for i := 0; i < 4; i++ {
                putt <- k
            }
            fmt.Println("comparing pricevalues", id, "Market open:", k)
            time.Sleep(time.Second)
            results1 <- k
        }
    }()
    return putt
}

func main() {
    fmt.Println("program starting")

    fmt.Println(orders2)

    job := make(chan string, 3)
    result := make(chan string, 3)

    for w := "a"; w <="f"; {
        go compcurrency(w, job, result)
    }
    for j := 1; j <= 9; j++ {
        job <- j
    }
    close(job)
    for a := 1; a <= 9; a++ {
        <-result
    }

    job1 := make(chan float32, 3)
    result1 := make(chan float32, 3)

    for x := 1; x <= 2; x++ {
        go comppricevalue(x, job1, result1)
    }
    for k := 1; k <= 9; k++ {
        job1 <- k
    }
    close(job1)
    for a := 1; a <= 9; a++ {
        <-result1
    }
}

Kurtis Rader

unread,
Jan 28, 2023, 9:02:11 PM1/28/23
to yeshwanthchowdary jampala, golang-nuts
On Sat, Jan 28, 2023 at 8:05 AM yeshwanthchowdary jampala <yeshwanthchow...@gmail.com> wrote:
Hi everyone,

I'm getting this error:
1)cannot use j (variable of type int) as type string in send
2)cannot use x (variable of type int) as type float32 in argument to comppricevalue
3)cannot use k (variable of type int) as type float32 in send

Those errors seem clear enough to me. Consider the first one. It is telling you that at line 80 you are attempting to send an int on a channel you defined as passing strings (from line 73: `job := make(chan string, 3)`). If you want to pass integers through the channel change its creation to `job := make(chan int, 3)`. Alternatively, change the channel send from this

    for j := 1; j <= 9; j++ {
        job <- j
    }

to this

    for j := 1; j <= 9; j++ {
        job <- fmt.Sprintf("%d", j)  // or strconv.Itoa(j)
    }

Perhaps you were expecting Go to recognize the channel handled strings and automatically convert the int to a string (similar to how C has implicit type conversions). Go, for good reasons, does not do any implicit type conversions other than for untyped constants (see https://go.dev/ref/spec#Constants). 
 
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ae0ca8cd-3e71-4f5d-b1e4-755c6e24eeebn%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
Reply all
Reply to author
Forward
0 new messages