Repository: go
Issue: go/build: file name build constraints incorrect when name contains dots
Labels: NeedsInvestigation

<!--
Please answer these questions before submitting your issue. Thanks!
For questions please use one of our forums: https://github.com/golang/go/wiki/Questions
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.16.4 darwin/amd64
</pre>

### Does this issue reproduce with the latest release?

Yes

### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kyle.nusbaum/Library/Caches/go-build"
GOENV="/Users/kyle.nusbaum/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/kyle.nusbaum/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/kyle.nusbaum/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/kyle.nusbaum/sdk/go1.16.4"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/kyle.nusbaum/sdk/go1.16.4/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/kyle.nusbaum/Documents/CodeBase/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8x/bn6z9tk109n_hvbhl8fwdmpw0000gn/T/go-build558940160=/tmp/go-build -gno-record-gcc-switches -fno-common"
</pre></details>

### What did you do?
I created a `.go` file which contained dots in the name along with a `_GOOS` extension (e.g. `file.name_darwin.go`)

### What did you expect to see?
I expected this file to be compiled only when `GOOS=darwin`

Please see this example, which I expect to compile but does not:
https://github.com/knusbaum/goissue/


### What did you see instead?
The error that appears:
<pre>
% go build
# github.com/knusbaum/goissue
./my.name.result_linux.go:3:6: Result redeclared in this block
	previous declaration at ./my.name.result_darwin.go:3:15
</pre>

### Additional information

The [build constraint documentation](https://golang.org/cmd/go/#hdr-Build_constraints) says this:
<pre>
 If a file's name, after stripping the extension and a possible _test suffix, matches any of the following patterns:

*_GOOS
*_GOARCH
*_GOOS_GOARCH
</pre>

Wherever a file's "extension" is mentioned in the Go documentation, it means whatever comes after the final `.` in the file name. This is also what [path.Ext](https://pkg.go.dev/path?utm_source=godoc#Ext) documents and computes.

However, the os/arch-matching code takes the extension from the first instance of `.` rather than the last, as the documentation would lead me to expect.

This places requirements for using the os/arch constraints in file names that are not documented anywhere and are inconsistent with the way Go code and documentation usually deals with file extensions.