goで同一ディレクトリに、複数main.mainをおきたい

続編を書いた。

とりあえずここに落ち着いたGoの即時実行環境 -2014年冬- - コンドルが飛んでいる。


以下古いやつ

その場で書いて実行できて置いておける、みたいな場所が欲しい。go playgroundは便利だけど、それだけで十分かというと、そうではないので、適当なディレクリにスクリプト雑多においておいて go run a.go とか、go run b.go って出来るようにしておきたい。

それ自体はできるけど、Vim上とかで警告[1]されるのがつらい。

そういう時には、// +build ignore とか書いておいてビルド対象じゃなくすると良いっぽい。ignoreじゃなくても、コード書いてる環境がターゲットにならないような指定ならなんでもいいけど、ignoreって書いておくと通じやすいし良さそう。c.f. http://golang.org/pkg/go/build/#hdr-Build_Constraints

a.go

// +build ignore

package main

import (
    "fmt"
)

func main() {
    fmt.Println("b")
}

b.go

// +build ignore

package main

import (
    "fmt"
)

func main() {
    fmt.Println("b")
}

追記

importしたけど、使ってないpackageがある時とか未定義な変数使ったりしてる時の警告も出なくなるので、どっちが不便かわからない感じはする。

追記2

微妙な感じだけど、こんな感じで対処した。あと、:silent make <args> | cwindow 時に描画が変になるのをどうにかしたい。

augroup GoFile
  autocmd!
  " ....
  au BufWritePre *.go :call GoMakeSetting()
augroup END


function! GoMakeSetting()
  setl makeprg=go\ build
  augroup GoMakeSettingGroup
    au!
  augroup END

  if exists(":Make") != 2
    command -nargs=* Make silent make <args> | cwindow | redraw!
  endif

  if match(getline(1), "\/\/ *\+build *ignore") != -1
    setl makeprg=go\ build\ %

    augroup GoMakeSettingGroup
      au BufWritePost <buffer> :Make
      au WinEnter * if winnr('$') == 1 && getbufvar(winbufnr(winnr()), "&buftype") == "quickfix"|q|endif
    augroup END
  endif
endfunction
  • [1] main redeclared in this block previous declaration at みたいなやつ