Ruby Programmer's Golang Tour 01

Source: Internet
Author: User
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"
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.