Go 1.26 (not yet released) will allow the new built-in to be called on expressions: https://antonz.org/accepted/new-expr/
Therefore, the following Go Protobuf example:
package main
import (
"fmt"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)
func main() {
b, err := proto.Marshal(&descriptorpb.DescriptorProto{
Name: proto.String("hoi"),
})
if err != nil {
panic(err)
}
fmt.Printf("Protobuf wire format:\n% x\n", b)
}
…can be changed to use new("hoi"):
package main
import (
"fmt"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)
func main() {
b, err := proto.Marshal(&descriptorpb.DescriptorProto{
Name: new("hoi"),
})
if err != nil {
panic(err)
}
fmt.Printf("Protobuf wire format:\n% x\n", b)
}
This means that the need for the helper functions in the proto package like proto.String is going away over time.
We will not deprecate these functions because that causes unnecessary churn (and there’s nothing wrong with using the functions).
We should probably update the examples to use the new() syntax throughout, but not sure at which point in time. Immediately after the Go 1.26 release would probably be a little bit early.
I’m filing this issue for discussion. If there are any considerations with regards to how Go Protobuf should change with regards to the new built-in, please share them here.
Go 1.26 (not yet released) will allow the
newbuilt-in to be called on expressions: https://antonz.org/accepted/new-expr/Therefore, the following Go Protobuf example:
…can be changed to use
new("hoi"):This means that the need for the helper functions in the
protopackage likeproto.Stringis going away over time.We will not deprecate these functions because that causes unnecessary churn (and there’s nothing wrong with using the functions).
We should probably update the examples to use the
new()syntax throughout, but not sure at which point in time. Immediately after the Go 1.26 release would probably be a little bit early.I’m filing this issue for discussion. If there are any considerations with regards to how Go Protobuf should change with regards to the
newbuilt-in, please share them here.