LoginSignup
37
29

More than 5 years have passed since last update.

goのimport宣言

Last updated at Posted at 2014-08-01

さり気ないけれどGoで
importを宣言するとき、頭に._や名前をつけることが出来る。
それを使ってパッケージのメソッドにアクセスする。

package main

import (
  "fmt"
  . "fmt" // Println
  _ "fmt" // 利用なし
  f "fmt" // f.Println 
)

func main() {
  fmt.Println("Hello World") // Hello World
  Println("Hello World")     // Hello World
  f.Println("Hello World")   // Hello World
}

ちなみに _は、そのファイルでは使わないけれど、他パッケージで利用したい場合に使う。

参考

37
29
0

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
37
29