Underline in Go

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

1. Underline in Import

In the Golang, import is the role of importing other package, but today in the look at the Beego framework saw the import underline, do not know its meaning, so Baidu and solution.

Import underline (e.g. Import _ Hello/imp): When importing a package, all init () functions are executed in the file under the package, however, sometimes we do not need to import the entire package, just want it to execute the init () function. You can use import to refer to the package at this time. Even if you refer to the package with the import _ package path only to invoke the Init () function, you cannot call other functions in the package by the package name.
Example:
Code structure

Main.go

package mainimport"hello/imp"func main() {    //imp.Print() //编译报错,说:undefined: imp}

Init.go

package impimport"fmt"func init() {    fmt.Println("imp-init() come here.")}func Print() {    fmt.Println("Hello!")}

Output Result:

imp-init() come here.

2. Underline in code

    package main    import (        "os"    )    func main() {        make([]byte, 1024)        f, _ := os.Open("/Users/samchen/Music/text.txt")        defer f.Close()        for {            n, _ := f.Read(buf)            if n == 0 {                break        }        os.Stdout.Write(buf[:n])    }

Explanation 1:

Underlining means ignoring this variable.

such as the OS. Open, the return value is *os. File,error

The general notation is f,err: = OS. Open (xxxxxxx)

If you do not need to know the returned error value at this time

You can use F, _: = OS. Open (XXXXXX)

This ignores the error variable

Explanation 2:

placeholder, meaning that the position should have been assigned to a value, but we do not need this value, so the value is assigned to underline, meaning that the compiler can better optimize, any type of a single value can be dropped to underline.
This is a placeholder, the method returns two results, and you want only one result, and the other uses _ placeholder, and if the variable is not used, the compiler will error.

Add:

    import"database/sql"    import"github.com/go-sql-driver/mysql"

The second import is to not use the MySQL package directly, just execute the init function of the package, register the MySQL driver into the SQL package, and then the program can use SQL packets to access the MySQL database.

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.