Site Search

2010-09-28

Go/Golang: Regex and program execution demo

package main

import (
    "exec"
    "flag"
    "fmt"
    "os"
    "regexp"
)

const UserHomeDir = "/Users/alex"
const Compiler = UserHomeDir + "/my/dev/env/go/bin/6g"
const Linker =  UserHomeDir + "/my/dev/env/go/bin/6l"
const Pwd = UserHomeDir + "/my/dev/prj/loxal/example/src/main/golang/"

func compile() os.Error {
    cmd, err := exec.Run(Compiler, []string{Compiler, flag.Arg(0)}, nil, "./",
        exec.DevNull, exec.DevNull, exec.PassThrough)
    if err != nil {
        return err
    }
    return cmd.Close()
}

func link() os.Error {
    cmd, err := exec.Run(Linker, []string{Linker, "-o" + getFileStem(flag.Arg(0)) + ".out", getFileStem(flag.Arg(0)) + ".6"}, nil, "./",
        exec.DevNull, exec.DevNull, exec.PassThrough)

    if err != nil {
        return err
    }
    return cmd.Close()
}

func getFileStem(fileName string) string {
    var pattern *regexp.Regexp = regexp.MustCompile("^(.*)\\.go$")

    return pattern.FindStringSubmatch(fileName)[1]
}

func main() {
    fmt.Printf("NArg: %v\n", flag.NArg())

    compile()
    link()
}