First glance in Go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Because I forgot to install the Chinese input in this OS, I has to use 中文版 input.

The first problem which I ran into was "how to import the local file in Golang"

First, it may is my fault but I really didn ' t see any documents say so if you want to import a local file in another Go File, you should create a folder to put the imported file. Below is my case:

I created a file said "Example2.go", then typed some codes:

int int {    return A + B}

I wanted to import it in another file said "Example1.go"

Package Mainimport (        "fmt"        "./example2"        ) Func main () {    fmt. Println ("Hello go! " )    FMT. Println (example2. ADD (1,2))}

When I tried to run the Example1.go file, the go runtime threw an error said "couldn ' t find the example2". What ' s the problem? Is Gopath not set? Is Example2.go not compiled? Finally, I found it is the folder structure problem. My orignal folder structure likes this

GOWORKSPACE-PKG-SRC    -Example1.go    -Example2.go-bin

But Go 1.4 doesn ' t support import a single file as a package. So I had to put the ' Example2.go ' in a folder said "Package2"

GOWORKSPACE-PKG-SRC    -Example1.go    -Package2        -Example2.go-bin

Then change the code

Package Mainimport (        "fmt"        "package2 "        ) Func main () {    fmt. Println ("Hello go! " )    FMT. Println (example2. ADD (1,2))}

It works. So it means a, refers to a, folder in the Golang.

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.