Go – how can i do this python code in go code?

huangapple go评论96阅读模式
英文:

Go - how can i do this python code in go code?

问题

你好!以下是使用Go语言实现相同功能的示例代码:

package main

import (
	"fmt"
	"os/exec"
	"runtime"
	"time"
)

func main() {
	var cmd *exec.Cmd
	if runtime.GOOS == "windows" {
		cmd = exec.Command("cmd", "/C", "start", "chrome")
	} else if runtime.GOOS == "darwin" {
		cmd = exec.Command("open", "-a", "Google Chrome")
	} else {
		fmt.Println("Unsupported operating system")
		return
	}

	err := cmd.Start()
	if err != nil {
		fmt.Println("Failed to start Chrome:", err)
		return
	}

	time.Sleep(100 * time.Millisecond)

	err = cmd.Process.Signal(os.Interrupt)
	if err != nil {
		fmt.Println("Failed to activate Chrome window:", err)
		return
	}

	time.Sleep(100 * time.Millisecond)

	err = exec.Command("cmd", "/C", "echo www.stackoverflow.com | clip").Run()
	if err != nil {
		fmt.Println("Failed to copy URL to clipboard:", err)
		return
	}

	time.Sleep(100 * time.Millisecond)

	err = exec.Command("cmd", "/C", "start", "chrome", "--new-window").Run()
	if err != nil {
		fmt.Println("Failed to open new Chrome window:", err)
		return
	}

	time.Sleep(4 * time.Second)

	err = exec.Command("cmd", "/C", "start", "chrome", "--new-window", "--kiosk").Run()
	if err != nil {
		fmt.Println("Failed to enter full screen mode:", err)
		return
	}
}

请注意,此示例代码仅适用于Windows和Mac操作系统。如果您使用其他操作系统,需要根据具体情况进行修改。此外,您可能需要安装Go语言的相关依赖库。

希望对您有所帮助!如果您有任何其他问题,请随时提问。

英文:

How can i do this in Go language please? I have used Python 2.7, pywin32 for the win32com and the following code which works to trigger virtual keyboard press.

Python code works:

import time
import win32com
import win32com.client
shell = win32com.client.Dispatch('WScript.Shell')
shell.Run('chrome')    
time.sleep(0.1)
shell.AppActivate('chrome')
shell.SendKeys("www.stackoverflow.com", 0)
shell.SendKeys("{Enter}", 0)
time.sleep(4)
shell.SendKeys("{F11}", 0)

i am actually trying the same Python code in Go (for Windows and Mac), can anyone give me any example how exactly this can be done with Go?

Go – how can i do this python code in go code?

答案1

得分: 2

这不涉及到win32调用部分,我假设你可以通过syscall包来实现,就像这个例子中的https://github.com/AllenDang/w32一样。

至于以全屏模式启动Chrome,你可以通过快捷方式来实现,或者如果你真的想通过Go来实现,可以使用以下代码:

package main

import "os/exec"

func main(){
    chrome := "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
    cmd := exec.Command(chrome,"--chrome-frame","--kiosk","http://www.ibm.com")
    err := cmd.Start()
    if err != nil {
        println("Failed to start chrome:", err)
    }
}
英文:

This doesn't answer the win32 calls part, I assume you can do that via the syscall package, as in https://github.com/AllenDang/w32

As for launching chrome in full screen mode, you can do from a shortcut or if you really want to do it from go, you can do: Play (can't run there)

package main
import "os/exec"
func main(){
chrome := "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
cmd := exec.Command(chrome,"--chrome-frame","--kiosk","http://www.ibm.com")
err := cmd.Start()
if err != nil {
println("Failed to start chrome:", err)
}
}

答案2

得分: 0

win32 ole实现的golang版本
https://github.com/go-ole/go-ole

英文:

win32 ole implementation for golang
https://github.com/go-ole/go-ole

huangapple
  • 本文由 发表于 2014年1月26日 22:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/21364498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定