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: spec: allow conversion from array to slice #70941

Closed
debugger-zhang opened this issue Dec 20, 2024 · 3 comments
Closed

proposal: spec: allow conversion from array to slice #70941

debugger-zhang opened this issue Dec 20, 2024 · 3 comments
Labels
Milestone

Comments

@debugger-zhang
Copy link

Proposal Details

This will be the inverse of #46505

Usually, you can use array[:] to create a slice from array. However this will not work if array is the return value of a function call (which is unaddressable). For example md5.Sum returns a [16]byte but hex.EncodeToString requires a []byte. Make [16]byte conversible to []byte will allow people to call hex.EncodeToString with result of md5.Sum without assigning the result to an immediate variable:

fmt.Println(hex.EncodeToString([]byte(md5.Sum([]byte("test"))))) // this is currently invalid

Proposed: if array is addressable, converting it to slice equals to array[:]. Otherwise the value of array is assigned to a new temporary variable x and x[:] is returned.

@gopherbot gopherbot added this to the Proposal milestone Dec 20, 2024
@gabyhelp
Copy link

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@seankhliao
Copy link
Member

Duplicate of #45653

@seankhliao seankhliao marked this as a duplicate of #45653 Dec 20, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 20, 2024
@zigo101
Copy link

zigo101 commented Dec 21, 2024

Poor man's way: [][md5.Size]byte{md5.Sum([]byte("test"))}[0][:], if you have to write in one line.

Or you can use nstd.New(foo())[:]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants