Go: proposal: Go 2: make no result values expected when self has no return value too

Created on 17 Jan 2020  路  5Comments  路  Source: golang/go

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

$ go version
go version go1.13.1 linux/amd64

Does this issue reproduce with the latest release?

yes

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

Ubuntu 16.04

go env Output

$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY="git.sysop.bigo.sg"
GONOSUMDB="git.sysop.bigo.sg"
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE="git.sysop.bigo.sg"
GOPROXY="https://goproxy.io,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/root/dev_volumn/dis-grep/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 -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build890909968=/tmp/go-build -gno-record-gcc-switches"

What did you do?

fake code is here:

func B() {
}

func A() {
return B()
}

What did you expect to see?

I want this works ok

What did you see instead?

It failed

In fact, A and B all have no return values, sometimes, for example in http handler, I need to cancel function with a 400 or something else:

func handler(ctx ...) {
...
// something went wrong
ctx.StatusCode(400)
return
}

But I can not just use return ctx.StatusCode(400) end this function, this make my code not clean, so, may golang make return with no value legal when function itself has no return value too?

thanks!!!

Go2 LanguageChange Proposal WaitingForInfo

Most helpful comment

How is

ctx.StatusCode(400)
return

less clean than

return ctx.StatusCode(400)

? They are roughly the same thing... except allowing the second one in a function that returns nothing would be misleading.

I think the confusion that allowing this syntax would cause would far exceed the small benefit of not having to write ctx.StatusCode(400) and return on two different lines.

All 5 comments

if A has a return int, and B has a return int, direct return B() is ok, so NO return params maybe should be same with NO return params I think, so such code can we same

How is

ctx.StatusCode(400)
return

less clean than

return ctx.StatusCode(400)

? They are roughly the same thing... except allowing the second one in a function that returns nothing would be misleading.

I think the confusion that allowing this syntax would cause would far exceed the small benefit of not having to write ctx.StatusCode(400) and return on two different lines.

ok, thanks

For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/bd3ac287ccbebb2d12a386f1f1447876dd74b54d/go2-language-changes.md .

When you are done, please reply to the issue with @gopherbot please remove label WaitingForInfo.

Thanks!

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

Was this page helpful?
0 / 5 - 0 ratings