Go Win Stuff
Nov 16, 2012No, not contests, golang (the programming language), and Win as in Windows.
Quick background - Recently I started writing a MUD in Go for the purposes of learning Go, and writing something that is non-trivial to code. MUDs are particularly suited to Go, since they are entirely server based, are text-based, and are highly concurrent and parallel problems (which is to say, you have a whole bunch of people doing stuff all at the same time on the server).
Anyway, after getting a pretty good prototype of the MUD up and running (which was quite fun), I started thinking about using Go for some scripty things that I want to do at work. There’s a bit of a hitch, though… the docs on working in Windows are not very good. In fact, if you look at golang.org, they’re actually non-existent. This is because the syscall package changes based on what OS you’re running on, and (not surprisingly) Google’s public golang site is not running on Windows.
So, anyway, a couple notes here on Windowy things that you (I) might want to do with Go:
Open the default browser with a given URL:
import (Example of a wrapper for syscall’s Windows Registry functions:
“syscall/exec”
)
func OpenBrowser(url string) {
exec.Command(“rundll32”, “url.dll”, “FileProtocolHandler”, url)
}
import (
“syscall”
)
func ReadRegString(hive syscall.Handle, subKeyPath, valueName string) (value string, err error) {
var h syscall.Handle
err = syscall.RegOpenKeyEx(hive, syscall.StringToUTF16Ptr(subKeyPath), 0, syscall.KEY_READ, &h)
if err != nil {
return
}
defer syscall.RegCloseKey(h)
var typ uint32
var bufSize uint32
err = syscall.RegQueryValueEx(
hKey,
syscall.StringToUTF16Ptr(valueName),
nil,
&typ,
nil,
&bufSize)
if err != nil {
return
}
data := make([]uint16, bufSize/2+1)
err = syscall.RegQueryValueEx(
hKey,
syscall.StringToUTF16Ptr(valueName),
nil,
&typ,
(*byte)(unsafe.Pointer(&data[0])),
&bufSize)
if err != nil {
return
}
return syscall.UTF16ToString(data), nil
}