Use context to stop sending values rather than trying to detect channel close
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"encoding/gob"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net"
 | 
			
		||||
@@ -12,21 +13,23 @@ import (
 | 
			
		||||
func main() {
 | 
			
		||||
	gob.Register([2]int{})
 | 
			
		||||
 | 
			
		||||
	ctx := context.Background()
 | 
			
		||||
 | 
			
		||||
	conn, _ := net.Dial("tcp", "localhost:9090")
 | 
			
		||||
	c := client.New(conn, codec.Gob)
 | 
			
		||||
	defer c.Close()
 | 
			
		||||
 | 
			
		||||
	var add int
 | 
			
		||||
	c.Call("Arith", "Add", [2]int{5, 5}, &add)
 | 
			
		||||
	c.Call(ctx, "Arith", "Add", [2]int{5, 5}, &add)
 | 
			
		||||
 | 
			
		||||
	var sub int
 | 
			
		||||
	c.Call("Arith", "Sub", [2]int{5, 5}, &sub)
 | 
			
		||||
	c.Call(ctx, "Arith", "Sub", [2]int{5, 5}, &sub)
 | 
			
		||||
 | 
			
		||||
	var mul int
 | 
			
		||||
	c.Call("Arith", "Mul", [2]int{5, 5}, &mul)
 | 
			
		||||
	c.Call(ctx, "Arith", "Mul", [2]int{5, 5}, &mul)
 | 
			
		||||
 | 
			
		||||
	var div int
 | 
			
		||||
	c.Call("Arith", "Div", [2]int{5, 5}, &div)
 | 
			
		||||
	c.Call(ctx, "Arith", "Div", [2]int{5, 5}, &div)
 | 
			
		||||
 | 
			
		||||
	fmt.Printf(
 | 
			
		||||
		"add: %d, sub: %d, mul: %d, div: %d\n",
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user