This is a creation in Article, where the information may have evolved or changed.
Golang's import @ Ruby's require
We're just taking this opportunity to review Ruby's require first.
require(name) → true or false
Require loads the specified file, returns True if the load succeeds, or false if it has already been loaded.
require 'csv'=> truerequire 'csv'=> false
If the filename resolution is not an absolute path, it will be looked up in the directory listed $MY _ruby_home.
require 'foo'=> cannot load such file -- foo from /Users/jiyarong/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Users/jiyarong/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' from (irb):3 from /Users/jiyarong/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
And look at the concept of Golang ' package '.
Each Go program is made up of packages.
The entry that the program runs is the package main
.
package mainimport "fmt"func main() { fmt.Println("Hello, 世界")}
After you have imported a package, you can call it with its exported name.
For example, after the import "fmt"
, you can use fmt
this prefix to invoke the FMT package inside the Println
function, forced to use Ruby to explain the words, fmt
presumably can be understood as a class
bar, Println
is a class method of this class
$ go run hello.go Hello, World!
You can also import multiple files at once
package mainimport ( "fmt" "math")func main() { fmt.Printf("Now you have %g problems.", math.Nextafter(2, 3))}
import ( "fmt" "math")等同于import "fmt"import "math/rand"