首页 > 解决方案 > 外部包函数的返回通道问题

问题描述

下面的代码是我正在做的工作的一部分。执行“main”函数,但不打印调用函数返回的映射,该函数位于另一个包中。我对最后一个函数进行了测试,它确实产生了回报。我找不到我的错误。

func main() {
    // This is part of the code
    // The channel "dataEntry" and "channelTJD", and the "waitgroup", was created previously. There are return channels of other functions.

    PositionPlsChan := make(chan map[float64]map[int]map[int][]float64, 1)

    // Code to verify that the channel works
    if PositionPlsChan == nil {
        fmt.Println("\nPositionPlsChan is NIL")
    } else {
        fmt.Println("\nPositionPlsChan is NOT nil") // Print this option when run the code
    }

    func() {
        go func() {
            // Call the function of the other package
            plnts.PlntsUT(dataEntry, channelTJD, &espera, PositionPlsChan)
            }()
        
        for {
            // Code to verify that the channel works
            select {
            case receives, ok := <-PositionPlsChan:
                if ok {
                    fmt.Println("receives", receives) // It should print the value contained in the channel
                } else {
                    fmt.Println("Channel cerrado")
                }
            default:
                println("There is not value in channel. Waiting.") // but print this option
            }
        }       
    }()
}

其他套餐

此代码基于我所做的检查工作

package plnts

func PlntsUT(

    dataEntryChan chan entradadatos.DataEntry, 
    channelTJD chan float64, 
    wg *sync.WaitGroup, 
    ChannelReturn chan map[float64]map[int]map[int][]float64,
) {
    dataEntry := <-dataEntryChan // Receive the channel
    tjd := <-channelTJD // Receive the channel
    
    // Create a map from maps
    var plntsMap = map[float64]map[int]map[int][]float64{}
 
   //Initialize the map of maps
    plntsMap[tjd] = map[int]map[int][]float64{}
    
    for _, bodieValue := range dataEntry.Plnts { // Plnts is []int
        // Channel to send as argument
        var sendData = make(chan receivesData, 1)
        defer close(sendData) 

        //Channel to send and receive the return of the function to call
        var receivesPlsChan = make(chan []float64, 1)
        defer close(receivesPlsChan) 

        // Initialize map of maps
        plntsMap[tjd][bodieValue] = map[int][]float64{}
        
        for _, iflagValue := range dataEntry.Sistem { // Sistema is []int                           
            variableStruct := receivesData{
                tjd:   tjd,
                bodie: bodieValue,
                iflag: iflagValue,              
            }
            
            sendData <- variableStruct
            
            go func() {
                    // Call another function in the same package
                calcUnPlntUT(sendData, receivesPlsChan)
            }()

            //Send the channel value to the map
            plntsMap[tjd][bodieValue][iflagValue] = <-receivesPlsChan           
        }
    }

    // Initialize the map
    ChannelReturn = make(chan map[float64]map[int]map[int][]float64, 1)

    if ChannelReturn == nil {
        fmt.Println("channelReturn is NIL")
    } else {
        fmt.Println("channelReturn is NIL") // Print this option
    }
    
    ChannelReturn <- plntsMap // The channel received the map value
    
    // Code to verify that the channel works
    // This portion of code will be commented (//),
    // so that it is not executed, after verifying 
    // that the channel receives the value.

    // select {
    // case _, ok := <-ChannelReturn:
    //      if ok {
    //          fmt.Println("Value in channel") // Print this option
    //      } else {
    //          fmt.Println("Channel Closed")
    //      }
    //      default:
    
    println("No value in channel")
    }

    // Code to verify that the map has value
    fmt.Println("\n map of maps", plntsMap) // Print the map of maps
}

输出:

PS C:\Go-Project\src\Ast> go run main.go
Main 1. It receives the channel dataEntry and is sent as a parameter to PlntsUT()

Main 2. channelTJD:  2.4400405e+06 

Main 3. PositionPlsChan was created. It is not nil

Main 4. Into goroutine. PlntsUT() is called. Sent parameters: dataEntry, channelTJD, & wait, PositionPlsChan.

               4.1 PlntsUT() - Start function
               4.2 PlntsUT() - The map was sent to the return channel
               4.3 PlntsUT() - Print map just before returning function: map[2.4400405e+06:map[0:map[33026:[101.12041750507854 -0.001946848541607366 1.0167564776934892 0.9653730337078059 0.0017513658174335596 -4.0388756783471536e-05]] 1:map[33026:[183.9503825565266 0.06704869143353376 0.0025344011723499632 14.97294331983312 -0.403467984791267 0.00017180324011524528]] 2:map[33026:[83.55721667001407 -4.271338209324602 0.6859570219539189 0.23997190595537177 0.11856886765748698 0.016097833472894507]]]]

Main 5. PlntsUT() was called

Main 6. Into select, PositionPlsChan pass its value
to the variable receives and checks it. This is the select code:

            for {
                 select {
                         case receives, ok := <-PositionPlsChan:
                                 if ok {
                                         fmt.Println("\nMain 7. Chek: ok== true: receives =", receives)
                                 } else {
                                         fmt.Println("\nMain 7. Chek: ok!= true: The variable receives is not printed:")
                                 }
                         default:
                                 fmt.Println("Main 7. Chek (select). Default option. There is no value in the channel. Waiting")
                                                                time.Sleep(time.Millisecond * 100)

                                                        }

Main 7. Chek (select). Default option. There is no value in the channel. Waiting
Main 7. Chek (select). Default option. There is no value in the channel. Waiting
Main 7. Chek (select). Default option. There is no value in the channel. Waiting
Main 7. Chek (select). Default option. There is no value in the channel. Waiting
Main 7. Chek (select). Default option. There is no value in the channel. Waiting
Main 7. Chek (select). Default option. There is no value in the channel. Waiting
Main 7. Chek (select). Default option. There is no value in the channel. Waiting
Main 7. Chek (select). Default option. There is no value in the channel. Waiting
Main 7. Chek (select). Default option. There is no value in the channel. Waiting
````

标签: goreturn-valuechannel

解决方案


推荐阅读