我想在 Go 中转换此 Python 代码:
#!/usr/bin/python3
import sys
import dbus
if (len(sys.argv) < 2):
print("Usage: %s <modem> <ussd-string>" % (sys.argv[0]))
sys.exit(1)
bus = dbus.SystemBus()
path = sys.argv[1]
ussdstring = sys.argv[2]
ussd = dbus.Interface(bus.get_object('org.ofono', path),
'org.ofono.SupplementaryServices')
properties = ussd.GetProperties()
state = properties["State"]
if state == "idle":
result = ussd.Initiate(ussdstring, timeout=100)[1]
elif state == "user-response":
result = ussd.Respond(ussdstring, timeout=100)
else:
sys.exit(1);
properties = ussd.GetProperties()
state = properties["State"]
print('USSD RESPONSE:\n', result)
print('USSD SESSION:\n', state)
我尝试使用 github.com/guelfey/go.dbus 库:
package main
import (
"fmt"
"os"
"github.com/guelfey/go.dbus"
)
func main() {
fmt.Printf("DBUS Test.\n")
conn, err := dbus.SessionBus()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to connect to session bus:", err)
os.Exit(1)
}
busObject := conn.Object("org.ofono", "/ril_0")
fmt.Println("busObject:", busObject)
var list []string
busObject.Call("org.ofono.SupplementaryServices.Initiate", 0, "#101#").Store(&list)
fmt.Println("list:", list)
for _, v := range list {
fmt.Println(v)
}
}
但我得到了以下响应:
DBUS Test.
Failed to connect to session bus: user: Current not implemented on linux/arm
你知道如何使用这个DBUS库吗?这个库是 ARM7 上 go 的最佳库吗?
谢谢
最佳答案
此特定问题就在错误消息中:
... user: Current not implemented on linux/arm"
user.Current 仅在进行身份验证时调用,因此如果您提供自己的 Auth它不会调用 user.Current 的方法。
看起来您必须创建自己的 conn,而不是使用全局 sessionBus。 (有关更多详细信息,请参阅 SessionBus 和 Conn.Auth 的来源)
conn, err := dbusSessionBusPrivate()
if err != nil {
return
}
auths := []dbus.Auth{dbus.AuthExternal(username), dbus.AuthCookieSha1(username, homedir)}
if err := conn.Auth(auths); err != nil {
conn.Close()
return
}
您还可以修补 go.dbus 以使用替代方法来查找 arm 的用户名和主目录,例如检查 $USER 和 $HOME(或提交问题,或打开拉取请求)。
关于Golang 和 DBUS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35020486/
华为Od必看系列华为OD机试全流程解析+经验分享,题型分享,防作弊指南)华为od机试,独家整理已参加机试人员的实战技巧华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典文章目录华为Od必看系列使用说明本期题目:任务总执行时长题目输入输出示例一输入输出说明go代码实现华为OD其它语言版本
目录Golang获取HTTP请求的IP地址HTTP的发展历史3,HTTP所在的网络层次4,HTTP请求与响应
关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭4年前。Improvethisquestion在golang代码中使用sql查询获取语法错误。golang中此SQL查询所需的正确语法:rows,errQuery:=dbCon.Query("SELECTB.LatestDate,A.SVRNameASServerName,A.DRIVE,A.Tot
给定表达式字符串exp,编写程序检查exp中“{”、“}”、“(”、“)”、“[”、“]的对和顺序是否正确。packagemainimport("fmt"stack"github.com/golang-collections/collections/stack")funcmain(){s:="(a[0]+b[2c[6]])){24+53}"stackO:=stack.New()stackmap:=map[string]string{"[":"]","(":")","{":"}"}varstr=""for_,num:=ranges{str=string(num)if(str=="{"||
关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭3年前。Improvethisquestion如何通过使用此库在golang中传递数字来获取国家代码:https://godoc.org/github.com/nyaruka/phonenumbers?
为什么下面的代码会抛出意外的函数错误?我看到错误./func_correct.go:4:syntaxerror:unexpectedfunc,expectingnamepackagemainfunc(st*Stack)Pop()int{v:=0forix:=len(st)-1;ix>=0;ix--{ifv=st[ix];v!=0{st[ix]=0returnv}}return0}funcmain(){Pop()} 最佳答案 定义堆栈类型在main中为其创建一个变量对其调用Pop代码:packagemainimport"fmt"typ
Thisquestionalreadyhasanswershere:ForloopoftwovariablesinGo(3个答案)2年前关闭。我正在通过Gotour在Go中使用for循环我跑的时候packagemainimport"fmt"funcmain(){sum:=1forsum程序运行正常,输出为1024但是当我更改sum:=0时packagemainimport"fmt"funcmain(){sum:=0forsum它给出了错误的说法processtooktoolongProgramexited.编辑:我沉迷于Go巡回赛,以至于我无法意识到,我犯了一个逻辑错误:P。
关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭4年前。Improvethisquestion我有一个Json:{"id":"me","name":"myname","planets":{"EARTH":3,"MARS":4}}我不知道如何将planets字段解码为map[string]int,所以我将访问元素而无需解码它们,就像在这个例子中一样
我是Go(Golang)的新手。我写了一个简单的基准程序来测试MySQL的并发处理。当我增加并发channel数时,不断收到“dialtcp52.55.254.165:3306:getsockopt:connectionrefused”、“unexpectedEOF”错误。每个go例程都将1到n行批量插入到一个简单的客户表中。该程序允许设置可变插入大小(单个语句中的行数)和并行go例程的数量(每个go例程执行上面的一个插入)。程序在小数字row寻找线索。基于它们,我设置了数据库最大连接数以及“max_allowed_packet”和“max_connections”。我还设置了go
这个问题在这里已经有了答案:Functiondeclarationsyntax:thingsinparenthesisbeforefunctionname(3个答案)WhatsthedifferenceoffunctionsandmethodsinGo?(5个答案)ParameterbeforethefunctionnameinGo?[duplicate](1个回答)WhatdothebracketsafterfuncmeaninGo?[duplicate](1个回答)关闭8个月前。我正在学习Golang-在教程中我经常看到这样的语法:typeSomeTypestruct{//stru