Skip to content

new(expr) and its effect on Go Protobuf #1702

@stapelberg

Description

@stapelberg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions