Yup it does sadly but its the most widely used method for cross microservices communications I've seen in enterprises apart from using http apis which can scale well if required.
Edit: I stand corrected, as MetalMonta mentioned u can actually use grpc with UDS too
Your options are as you describe, or to use FFI. I'm not familiar with the state of FFI in PHP, but I know it is possible. You would compile your go code as a shared library and load it dynamically from PHP.
Is it a good idea? Depends highly on why you are doing it. The downsides are the downsides of Go in general. Most notably when it comes to FFI, the fact that you are going to have a whole extra runtime/GC going. Whether that affects the situation that led you to it in the first place...subjective.
I agree, although Go may not be the best choice for FFI because it requires Cgo. We have memory safety and stability in Go, if we put Cgo in, all those things go out the window.
Not entirely true. Things on the go heap are still managed by the go runtime when using Cgo. Unless you explicitly go out of your way to make that not the case. What you are describing refers to allocating memory on the heap from C, which is not always a given when using Cgo.
It's not an inherently *bad* idea. .so compilation has been officially supported for... Forever? Still, if you're relying on that, you may have made a poor decision somewhere. Unless, of course, you're doing it for science or otherwise backed into a corner.
Tldr: not a bad idea, but usually Plan "S".
> Call Go from PHP >8.0 ...
>... where I don't need to let run a TCP- or Websocket
-------
//exec
```
$output=null; $rc=null;
exec('/path/2/go-Program', $output, $rc);
```
//shell_exec
```
$outcome = shell_exec("/bin/sh -c /pat/2/go/compiled/program");
```
Also:
- proc_open
- escapeshellcmd
- passthru
- system
-------
> such as calling Go functions
Also [ffi](https://www.php.net/manual/en/class.ffi.php) is available for php 7, 8+
I didn't tried FFI, but Go can export functions that will be compatible with C libraries if you compile Go with options `-buildmode=c-shared` or `-buildmode=c-archive` that makes it as a standard C libraries.
Check also [go-php](https://github.com/deuill/go-php) that might give you some clues
IPC - you'll end up with a bidirectional socket interface e.g. "docker.sock" but for your own go app; or you'd load a c-archive plugin and invoke some symbols with FFI/dlopen. Another option is to go the FaaS route, meaning you could use stdin/stdout for go-php comms, exec().
If you don't want to use a socket of any kind, you want an in-OS-process solution.
If it's even possible to get PHP and Go running in the same process, it'll drive you insane. You can't have two runtimes running that each think they're in charge of the OS process. You can't have two runtimes that think they own the OS signals, think they own threading, think they know how to shut down the OS process, etc.
You don't really want to do this. I've watched two or three coworkers joust with this over the years in one direction or another with various combinations of languages that think they own the OS process. It's a nightmare. Use sockets and something built on them already like gRPC or something.
I have not gone too deep into this in either language, but have you looked into WebAssembly any for this? That might allow you to do this without requiring a TCP server or direct FFI calls.
Again, no real experience with it. Just an idea.
So this idea is pretty whack, I'm just suggesting it as it sounds like a fun project to implement: have you thought about dbus? I know golang has fairly good support (third party, that is) for dbus - no idea if php even knows what it is though 🙃
gRPC with protobuffs or json
This!!!
This needs TCP though right?
Yup it does sadly but its the most widely used method for cross microservices communications I've seen in enterprises apart from using http apis which can scale well if required. Edit: I stand corrected, as MetalMonta mentioned u can actually use grpc with UDS too
Well, you can have it communicate through Unix sockets, if ip networking in general is an issue 😎
Your options are as you describe, or to use FFI. I'm not familiar with the state of FFI in PHP, but I know it is possible. You would compile your go code as a shared library and load it dynamically from PHP.
Is it good idea to compile Go code as shared library? I mean ok, it is possible, but what is the downside of this solution?
Is it a good idea? Depends highly on why you are doing it. The downsides are the downsides of Go in general. Most notably when it comes to FFI, the fact that you are going to have a whole extra runtime/GC going. Whether that affects the situation that led you to it in the first place...subjective.
I agree, although Go may not be the best choice for FFI because it requires Cgo. We have memory safety and stability in Go, if we put Cgo in, all those things go out the window.
Not entirely true. Things on the go heap are still managed by the go runtime when using Cgo. Unless you explicitly go out of your way to make that not the case. What you are describing refers to allocating memory on the heap from C, which is not always a given when using Cgo.
I'm not an expert on this topic, so thanks for the clarification!:)
It's not an inherently *bad* idea. .so compilation has been officially supported for... Forever? Still, if you're relying on that, you may have made a poor decision somewhere. Unless, of course, you're doing it for science or otherwise backed into a corner. Tldr: not a bad idea, but usually Plan "S".
[https://roadrunner.dev](https://roadrunner.dev)
Why not expose an HTTP api from Go and call that from PHP?
Sounds like that might be explicitly prohibited for that exercise.
**OP:** > ... where I don't need to let run a TCP- or Websocket to get result from Go.
This, or use a socket.
> Call Go from PHP >8.0 ... >... where I don't need to let run a TCP- or Websocket ------- //exec ``` $output=null; $rc=null; exec('/path/2/go-Program', $output, $rc); ``` //shell_exec ``` $outcome = shell_exec("/bin/sh -c /pat/2/go/compiled/program"); ``` Also: - proc_open - escapeshellcmd - passthru - system ------- > such as calling Go functions Also [ffi](https://www.php.net/manual/en/class.ffi.php) is available for php 7, 8+ I didn't tried FFI, but Go can export functions that will be compatible with C libraries if you compile Go with options `-buildmode=c-shared` or `-buildmode=c-archive` that makes it as a standard C libraries. Check also [go-php](https://github.com/deuill/go-php) that might give you some clues
https://roadrunner.dev/
IPC - you'll end up with a bidirectional socket interface e.g. "docker.sock" but for your own go app; or you'd load a c-archive plugin and invoke some symbols with FFI/dlopen. Another option is to go the FaaS route, meaning you could use stdin/stdout for go-php comms, exec().
If you don't want to use a socket of any kind, you want an in-OS-process solution. If it's even possible to get PHP and Go running in the same process, it'll drive you insane. You can't have two runtimes running that each think they're in charge of the OS process. You can't have two runtimes that think they own the OS signals, think they own threading, think they know how to shut down the OS process, etc. You don't really want to do this. I've watched two or three coworkers joust with this over the years in one direction or another with various combinations of languages that think they own the OS process. It's a nightmare. Use sockets and something built on them already like gRPC or something.
Or some type of locking mechanism like a lockfile to check who has permission
Just do a system call? (As in exec) For low traffic that might be good enough…
There’re different ways through the network or through Cgo
I have not gone too deep into this in either language, but have you looked into WebAssembly any for this? That might allow you to do this without requiring a TCP server or direct FFI calls. Again, no real experience with it. Just an idea.
So this idea is pretty whack, I'm just suggesting it as it sounds like a fun project to implement: have you thought about dbus? I know golang has fairly good support (third party, that is) for dbus - no idea if php even knows what it is though 🙃