golang import all 类似python import * 效果

倖福魔咒の 提交于 2020-02-26 04:40:04
import "io/ioutil"

func main() { 
    content, err = iotuil.ReadFile("somefile.txt")
    // etc..
}

=》

I guess this doesn't really answer your question, but if you want, you can actually call the methods without explicitly stating the package - just import with a . in front of the names (but this is not recommended; see below):

package main

import (
  . "fmt"
  . "io/ioutil"
)

func main () {
  content, err := ReadFile("testfile")
  if err != nil {
    Println("Errors")
  }
  Println("My file:\n", string(content))
}

Note @jimt's comment below - this practice is not advised outside of tests as it could cause name conflicts with future releases. Also, definitely agree with @DavidGrayson's point of being nicer to read/see where things come from.

 

参考:https://stackoverflow.com/questions/12925450/importing-packages-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!