php Xiaobian Yuzai may encounter a common problem when developing applications using the Go language, that is, the "op" cannot be run from the Go application. This issue may cause the application to not function properly or to display an error message. There are many ways to solve this problem, which can be solved by checking whether the corresponding package is correctly imported in the code, or by reinstalling the relevant dependencies. Also, try other tools or methods to deal with the problem. I hope the introduction in this article can help developers who encounter this problem.
Question content
I'm trying to write a small go wrapper application around the 1password cli executable op
like this:
package main import ( "fmt" "log" "os/exec" ) func main() { op := exec.command("op", "item", "list") out, err := op.output() if e, ok := err.(*exec.exiterror); ok { log.fatal(e, ": ", string(e.stderr)) } fmt.println(string(out)) }
However, I keep getting the following error:
2023/04/13 09:48:51 exit status 1: [error] 2023/04/13 09:48:51 error initializing client: connecting to desktop app: read: connection reset, make sure the cli is correctly installed and connect with 1password cli is enabled in the 1password app
But when I do the same thing from a python script like this:
#!/usr/bin/env python import subprocess subprocess.run(["op", "item", "list"])
...I get nice output.
Interestingly, when I call the python script (named op.py
) from the go application, it works fine (the modified go application looks like this):
package main import ( "fmt" "log" "os/exec" ) func main() { op := exec.Command("/usr/bin/python3.11", "./op.py") //op := exec.Command("op", "item", "list") out, err := op.Output() if e, ok := err.(*exec.ExitError); ok { log.Fatal(e, ": ", string(e.Stderr)) } fmt.Println(string(out)) }
I can test that it is printed by the go application and not the python script because if I remove fmt.printf(...)
nothing is printed.
in conclusion:
- go ->
op
: does not work - python(
./op.py
)->op
: working fine - go -> python (
./op.py
) ->op
: working fine
Solution
I had the same problem when accessing op
from the pyenv
virtual environment. I think the problem is that the pyenv 's python
executable is owned by the user (me).
Changing ownership to root:the root of the python interpreter and the directory it resides in actually helps. Not sure what's going on behind the scenes.
Here are the steps (I used --copies
to create the virtual environment so it doesn't use symlinks - since symlinks would point to root-owned files):
$ python -m venv --copies .venv $ .venv/bin/python -c "import subprocess; subprocess.call(['op', 'signin'])" [ERROR] 2023/05/16 16:01:18 connecting to desktop app: read: connection reset, make sure the CLI is correctly installed and Connect with 1Password CLI is enabled in the 1Password app # chown -R root:root .venv/ $ .venv/bin/python -c "import subprocess; subprocess.call(['op', 'signin'])" Select account [Use arrows to move, type to filter]
Bottom line: Change the ownership of the executable file that spawned the op
child process (and the directory it resides in) to root:root
Also see this post on 1password which appears to be the same issue:
https://1password.community/discussion/135768/call-cli-from-a-go-program-
The above is the detailed content of Unable to run 'op' from Go application. For more information, please follow other related articles on the PHP Chinese website!

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

go语言能编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言。对Go语言程序进行编译的命令有两种:1、“go build”命令,可以将Go语言程序代码编译成二进制的可执行文件,但该二进制文件需要手动运行;2、“go run”命令,会在编译后直接运行Go语言程序,编译过程中会产生一个临时文件,但不会生成可执行文件。

go语言需要编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言,也就说Go语言程序在运行之前需要通过编译器生成二进制机器码(二进制的可执行文件),随后二进制文件才能在目标机器上运行。

删除字符串的方法:1、用TrimSpace()来去除字符串空格;2、用Trim()、TrimLeft()、TrimRight()、TrimPrefix()或TrimSuffix()来去除字符串中全部、左边或右边指定字符串;3、用TrimFunc()、TrimLeftFunc()或TrimRightFunc()来去除全部、左边或右边指定规则字符串。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
