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

vendor: type asserting vendored code VS $GOPATH code yield awkward experience #18706

Open
brenol opened this issue Jan 18, 2017 · 1 comment
Open
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@brenol
Copy link

brenol commented Jan 18, 2017

What version of Go are you using (go version)?

go version go1.7.4 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/brenol/goworkspace"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build237439903=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

A type assertion from a type inside another package that is actually vendored in that other package.

Given the following directory structure:

.
├── formatter
│   └── format.go
├── parser
│   ├── parser.go
│   └── vendor
│       └── github.com
│           └── brenol
│               └── formatter
│                   └── format.go
└── use_parser_and_formatter
    └── main.go

formatter/format.go:

package formatter

type Format struct{}

parser/parser.go

package parser

import "github.com/brenol/formatter"

func ReturnIt() interface{} {
	return formatter.Format{}
}

parser/vendor/github.com/brenol/formatter/format.go:

package formatter

type Format struct{}

use_parser_and_formatter/main.go:

package main

import (
	"fmt"

	"github.com/brenol/formatter"
	"github.com/brenol/parser"
)

func main() {
	v := parser.ReturnIt()
	switch val := v.(type) {
	case formatter.Format:
		fmt.Printf("I am a formatter.Format type: %T\n", val)
	default:
		fmt.Printf("I am a different type: %T\n", val)
	}
}

What did you expect to see?

I do not expect it to be of the same type - because they actually aren't - one is parser/vendor/github.com/brenol/formatter.Format and the another one is github.com/brenol/formatter/Format.

The behavior is correct, as expected. The problem is that this is cumbersome to track down and is very easy to miss. Perhaps %T should also show the package for that type, or the compiler should be more aggressive when checking for type assertions? I am not sure on how to improve the user experience on this one.

What did you see instead?

I am a different type: formatter.Format

@bradfitz bradfitz added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jan 19, 2017
@bradfitz bradfitz changed the title type asserting vendored code VS $GOPATH code yield awkward experience vendor: type asserting vendored code VS $GOPATH code yield awkward experience Jan 19, 2017
@bradfitz bradfitz added this to the Unplanned milestone Jan 19, 2017
@minux
Copy link
Member

minux commented Jan 19, 2017 via email

@rsc rsc removed their assignment Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

4 participants