22
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

あれ?go言語で実行ファイルのフルパス取れないの?

Last updated at Posted at 2014-06-25

Go 1.8 向け追記

os.Executableという標準関数で取得できるようになっているようです。

元記事

args0.go
package main

import "fmt"
import "os"

func main(){
        fmt.Printf("os.Args[0]=\"%s\"\n",os.Args[0])
}
[C:gosrc]
$ go run args0.go
os.Args[0]="C:\Users\Hayama\AppData\Local\Temp\go-build331085211\command-line-arguments\_obj\exe\args0.exe"

うん、go runだと問題ない。では、コンパイルしてみよう。

[C:gosrc]
$ go build args0.go
[C:gosrc]
$ args0.exe
os.Args[0]="args0.exe"
[C:gosrc]
$ cd ..
[C:\Opt]
$ gosrc\args0.exe
os.Args[0]="gosrc\args0.exe"

あれぇぇぇ?

(追記) 仕方ないので、モジュール書いた

2回もループ回したり、べたべたやけど

exename.go
package exename

//#include <windows.h>
import "C"

import "bytes"
import "unicode/utf16"

func Query() string {
	var pathW [C.MAX_PATH]C.WCHAR
	C.GetModuleFileNameW(nil, &pathW[0], C.MAX_PATH)

	var path16 [C.MAX_PATH]uint16
	for i := 0; pathW[i] != 0; i++ {
		path16[i] = (uint16)(pathW[i])
	}

	pathRune := utf16.Decode(path16[:])
	var buffer bytes.Buffer
	for _, ch := range pathRune {
		if ch == 0 {
			break
		}
		buffer.WriteRune(ch)
	}
	return buffer.String()
}
22
21
11

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?