Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
afanda0 opened this issue Jul 12, 2019 · 8 comments

Comments

@afanda0
Copy link

commented Jul 12, 2019

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

@bronze1man

This comment has been minimized.

Copy link
Contributor

commented Jul 12, 2019

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
@rocket049

This comment has been minimized.

Copy link

commented Jul 12, 2019

I oppose.

@afanda0

This comment has been minimized.

Copy link
Author

commented Jul 12, 2019

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

@afanda0 afanda0 changed the title Enhance the import for Go2 proposal: Go2 Enhance the import Jul 12, 2019

@gopherbot gopherbot added this to the Proposal milestone Jul 12, 2019

@gopherbot gopherbot added the Proposal label Jul 12, 2019

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jul 12, 2019

Related to #29036.

@ianlancetaylor ianlancetaylor changed the title proposal: Go2 Enhance the import proposal: Go 2: list symbols that don't require package name Jul 12, 2019

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Jul 12, 2019

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.

@afanda0

This comment has been minimized.

Copy link
Author

commented Jul 12, 2019

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

@yekhlakov

This comment has been minimized.

Copy link

commented Jul 17, 2019

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
}
@afanda0

This comment has been minimized.

Copy link
Author

commented Jul 17, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.