Go: proposal: Go 2: list symbols that don't require package name

Created on 12 Jul 2019  路  10Comments  路  Source: golang/go

Problem:

In many case, the last word of the import path is not the real package name. So, it is kind of confusing here ! And write the package name over and over is not the best way to write or read code.

So I bring a proposal:

Proposal1:

import (
    "bufio"
    "encoding/binary"
    "fmt"
    "hash/adler32"
    "io" {Reader IOReader} //Give a new name
    "os"

    "github.com/hashicorp/raft" {RPCHeader, WithRPCHeader} //import multi 
)

No new keywords come in, no break code.

Proposal2:

Proposal1 is probably confusing and not accepted commonly.

import  "bufio"
import  "encoding/binary"
import  "fmt"
import  "os"
import  "hash/adler32"
import  {Reader as IOReader} from  "io"  //Give a new name

import  {RPCHeader, WithRPCHeader} from "github.com/hashicorp/raft" 

This is totally copied from TypeScript

FrozenDueToAge Go2 LanguageChange Proposal Proposal-FinalCommentPeriod

Most helpful comment

You can get a similar effect by writing, after the imports,

type IOReader = io.Reader

(For a function, use a var. Admittedly there is no simple way to alias an exported package-scope variable, but those are not commonly used.)

The requirement of writing a package name is not a major complaint about Go. In fact, many people think it is a feature. I'm not sure it's worth changing the language to add a shorter way of doing something that you can already do, and that not a lot of people want to do anyhow.

Speaking only for myself.

All 10 comments

One problem in your suggest:

If follow three cases in the same project, there will be a burden for the code reader and make it harder to copy and paste small part of code between files:

  • rename io.Reader to IOReader in A file
  • rename io.Reader to IoReader in B file
  • use it with io.Reader in C file

I oppose.

@bronze1man Thank you, but I don't think that is a problem.

Related to #29036.

You can get a similar effect by writing, after the imports,

type IOReader = io.Reader

(For a function, use a var. Admittedly there is no simple way to alias an exported package-scope variable, but those are not commonly used.)

The requirement of writing a package name is not a major complaint about Go. In fact, many people think it is a feature. I'm not sure it's worth changing the language to add a shorter way of doing something that you can already do, and that not a lot of people want to do anyhow.

Speaking only for myself.

@ianlancetaylor Thank you. But I don't think it is a feature and the shorter way of coding is great.

Maybe it would be better to just allow omission of package name for all symbols that can be resolved unambiguously. Like so:

package main

import (
    "fmt"
)

// this shadows fmt.Print
func Print(x string) {
}

func main() {
    Println("Hello world") // instead of fmt.Println
    Print("local") // this package's Print is used
    fmt.Print("import") // package is specified explicitly
}

@yekhlakov Hello, I like that too. But trust me they will never let that happen!

As discussed above, there are other way to do this. We don't need a new syntax for it. And this proposal does not have strong support based on emoji voting. Therefore, this is a likely decline. Leaving open for four weeks for further comments.

There were no further comments.

Was this page helpful?
0 / 5 - 0 ratings