Home  >  Article  >  Backend Development  >  How to cooperate with php and golang

How to cooperate with php and golang

(*-*)浩
(*-*)浩Original
2019-10-17 13:39:056020browse

A scenario I encountered recently at work was that a third-party function needed to be used in the PHP project, and there happened to be a class library written in Golang. So the question becomes, how to achieve communication between different languages? Let’s take a look below.

How to cooperate with php and golang

Conventional solution (Recommended learning: PHP video tutorial)

1. Use Golang writes an http/TCP service, and php communicates with Golang through http/TCP

2. Golang is encapsulated as a php extension.

3. PHP calls the Golang executable file through system commands

Existing problems

1. HTTP requests and network I/O will consume a lot of time

2. A large amount of code needs to be encapsulated

3. Every time PHP calls a Golang program, it needs One initialization consumes a lot of time

Optimization goals

1. The Golang program is only initialized once (because initialization is time-consuming)

2. All requests do not need to go through the network

3. Try not to modify the code a lot

Solution

1. Simple Golang encapsulation, compile the third-party class library into an executable file

2. PHP and Golang communicate through a two-way pipeline

Advantages of using two-way pipeline communication

1: Only minimal encapsulation of the original Golang class library is required

2: Best performance (IPC communication is the best way to communicate between processes)

3: No need to make network requests, saving a lot of time

4: The program only needs to be initialized once, and Always kept in memory

Specific implementation steps

1: Original call demo in the class library

package main
import (
 "fmt"
 "github.com/yanyiwu/gojieba"
 "strings"
)
 
func main() {
 x := gojieba.NewJieba()
 defer x.Free()
 
 s := "小明硕士毕业于中国科学院计算所,后在日本京都大学深造"
 words := x.CutForSearch(s, true)
 fmt.Println(strings.Join(words, "/"))
}

Save the file as main .go, you can run

2: The adjusted code is:

package main
import (
 "bufio"
 "fmt"
 "github.com/yanyiwu/gojieba"
 "io"
 "os"
 "strings"
)
 
func main() {
 
 x := gojieba.NewJieba(
  "/data/tmp/jiebaDict/jieba.dict.utf8", 
  "/data/tmp/jiebaDict/hmm_model.utf8", 
  "/data/tmp/jiebaDict/user.dict.utf8"
 )
 defer x.Free()
 
 inputReader := bufio.NewReader(os.Stdin)
 for {
  s, err := inputReader.ReadString('\n')
  if err != nil && err == io.EOF {
   break
  }
  s = strings.TrimSpace(s)
 
  if s != "" {
   words := x.CutForSearch(s, true)
   fmt.Println(strings.Join(words, " "))
  } else {
   fmt.Println("get empty \n")
  }
 }
}

It only takes a few simple adjustments to achieve: receiving a string from the standard input , after word segmentation, output

Test:

# go build test
# ./test
# //等待用户输入,输入”这是一个测试“
# 这是 一个 测试 //程序

The above is the detailed content of How to cooperate with php and golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn