SoFunction
Updated on 2025-03-05

Detailed explanation of the future mode of Go language concurrency paradigm

1. Go language concurrency paradigm-future mode

In programming, multiple sub-calls need to be called in a process. These sub-calls have no dependence on each other. If called serially, it will take a long time. At this time, you can use the future mode in Go concurrent programming.

The basic working principle of future mode:

(1) Use chan as a function parameter.

(2) Start the goroutine call function.

(3) Pass parameters through chan.

(4) Do other things that can be handled in parallel.

(5) Obtain the results asynchronously through chan.

package main
import (
	"fmt"
	"time"
)
// A query structure// Here sql and result are a simple abstract, concrete application, which may be more complex data typestype query struct {
	//Parameter Channel	sql chan string
	//Result Channel	result chan string
}
//Execute Queryfunc execQuery(q query) {
	//Start the coroutine	go func() {
		//Get input		sql := <-
		//Access the database		//Output result channel		 <- "result from " + sql
	}()
}
func main() {
	//Initialize Query	q := query{make(chan string, 1), make(chan string, 1)}
	//Execute Query, please note that no parameters are required when executing	go execQuery(q)
	//Prepare parameters	 <- "select * from table;"
	//do otherthings
	(1 * )
	//Get the results	(<-)
}

Program output

result from select * from table;

The biggest benefit of future is to convert synchronous calls of functions into asynchronous calls, which is suitable for a transaction that requires multiple sub-calls and these sub-calls have no dependencies

scene. The actual situation may be much more complicated than the above example, and the handling of errors and exceptions is considered, and readers focus on experiencing this idea rather than details.

2. Steps to implement Future mode

(1) Build the structure FutureTask

Here we abstract what we want to do into a task. For each task we may need to pass parameters in the past, and we also need to get the execution result of this task. To do this, we create two channels, one for passing parameters and one for saving the result. (What other parameters are needed can be designed according to the specific business).

// FutureTask is used to pass parameters and save the returned results during concurrent executiontype FutureTask struct {
	// Used to pass parameters	args chan interface{}
	// There may be a lot of other data in the actual business	// Used to save the results	res chan interface{}
}

(2) Create a goroutine to execute future method

After creating the FutureTask, you need to enable goroutine to execute. For this reason, you need to create a method to execute FutureTask:

// execFutureTask is used to enable a future mode threadfunc execFutureTask(futureTask *FutureTask) {
	// Read the passed parameters	("Goroutine reads parameters:", <-)
	// Here you can execute specific business logic	result := "Results obtained after executing business logic"
	// Save the result	 <- result
	defer close()
	return
}

(3) Test code

package main
import (
	"fmt"
	"time"
)
// FutureTask is used to pass parameters and save the returned results during concurrent executiontype FutureTask struct {
	// Used to pass parameters	args chan interface{}
	// There may be a lot of other data in the actual business	// Used to save the results	res chan interface{}
}
// execFutureTask is used to enable a future mode threadfunc execFutureTask(futureTask *FutureTask) {
	// Read the passed parameters	("Goroutine reads parameters:", <-)
	// Here you can execute specific business logic	result := "Results obtained after executing business logic"
	// Save the result	 <- result
	defer close()
	return
}
func main() {
	// Create a FutureTask and enable a goroutine to execute	futureTask := FutureTask{make(chan interface{}), make(chan interface{})}
	go execFutureTask(&futureTask)
	// Pass parameters to FutureTask, if not, it will be locked	 <- "Parameters passed by main thread"
	// Here you can execute some other business logic in parallel	(1 * )
	// Read thread execution	("The main thread reads the result of goroutine in future mode:", <-)
}

(4) Execution results

Program output

Parameters read by goroutine: Parameters passed in the main thread
The main thread reads the result of goroutine in future mode: the result obtained after executing business logic

(5) Complete code

package main
import (
	"fmt"
	"time"
)
// FutureTask is used to pass parameters and save the returned results during concurrent executiontype FutureTask struct {
	// Used to pass parameters	args chan interface{}
	// There may be a lot of other data in the actual business	// Used to save the results	res chan interface{}
}
// execFutureTask is used to enable a future mode threadfunc execFutureTask(futureTask *FutureTask) {
	// Read the passed parameters	("Goroutine reads parameters:", <-)
	// Here you can execute specific business logic	result := "Results obtained after executing business logic"
	// Save the result	 <- result
	defer close()
	return
}
func main() {
	// Create a FutureTask and enable a goroutine to execute	futureTask := FutureTask{make(chan interface{}), make(chan interface{})}
	go execFutureTask(&futureTask)
	// Pass parameters to FutureTask, if not, it will be locked	 <- "Parameters passed by main thread"
	// Here you can execute some other business logic in parallel	(1 * )
	// Read thread execution	("The main thread reads the result of goroutine in future mode:", <-)
}

This is the end of this article about the detailed explanation of the future mode of the Go language concurrency paradigm. For more related content on Go future mode, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!