cmd/protoc-gen-go: use protoapi.CompressGZIP

Calling a helper function directly should reduce binary bloat slightly.

Change-Id: I6068dc4cd00c8d90d2e6e6d99633b81388bc8781
Reviewed-on: https://go-review.googlesource.com/c/164679
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index e842e6e..5357224 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -34,8 +34,6 @@
 const generatedCodeVersion = 3
 
 const (
-	bytesPackage    = protogen.GoImportPath("bytes")
-	gzipPackage     = protogen.GoImportPath("compress/gzip")
 	mathPackage     = protogen.GoImportPath("math")
 	protoPackage    = protogen.GoImportPath("github.com/golang/protobuf/proto")
 	protoapiPackage = protogen.GoImportPath("github.com/golang/protobuf/protoapi")
@@ -279,17 +277,9 @@
 	g.P("}")
 	g.P()
 
-	// TODO: Add a helper function in protoapi or protoimpl?
-	// This function would probably encode the input lazily upon first use.
-	// Currently, the GZIPed form is used eagerly in v1 registration.
-	// See https://play.golang.org/p/_atJHs0izTH
-	g.P("var ", f.descriptorGzipVar, " = func() []byte {")
-	g.P("bb := new(", bytesPackage.Ident("Buffer"), ")")
-	g.P("zw, _ := ", gzipPackage.Ident("NewWriterLevel"), "(bb, ", gzipPackage.Ident("NoCompression"), ")")
-	g.P("zw.Write(", f.descriptorRawVar, ")")
-	g.P("zw.Close()")
-	g.P("return bb.Bytes()")
-	g.P("}()")
+	// TODO: Modify CompressGZIP to lazy encode? Currently, the GZIP'd form
+	// is eagerly registered in v1, preventing any benefit from lazy encoding.
+	g.P("var ", f.descriptorGzipVar, " = ", protoapiPackage.Ident("CompressGZIP"), "(", f.descriptorRawVar, ")")
 	g.P()
 }