From 7d87f3d2338817fee1fac0332417fce5b95f563c Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Sat, 6 Aug 2011 11:21:59 +1000 Subject: [PATCH] FAQ: variant types, unions Fixes #1935. R=rsc, bradfitz CC=golang-dev https://golang.org/cl/4850044 --- doc/go_faq.html | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/doc/go_faq.html b/doc/go_faq.html index d7d23567e96..aeed5379567 100644 --- a/doc/go_faq.html +++ b/doc/go_faq.html @@ -695,6 +695,42 @@ for i, v := range t { } +

+Why are there no untagged unions, as in C?

+ +

+Untagged unions would violate Go's memory safety +guarantees. +

+ +

+Why does Go not have variant types?

+ +

+Variant types, also known as algebraic types, provide a way to specify +that a value might take one of a set of other types, but only those +types. A common example in systems programming would specify that an +error is, say, a network error, a security error or an application +error and allow the caller to discriminate the source of the problem +by examining the type of the error. Another example is a syntax tree +in which each node can be a different type: declaration, statement, +assignment and so on. +

+ +

+We considered adding variant types to Go, but after discussion +decided to leave them out because they overlap in confusing ways +with interfaces. What would happen if the elements of a variant type +were themselves interfaces? +

+ +

+Also, some of what variant types address is already covered by the +language. The error example is easy to express using an interface +value to hold the error and a type switch to discriminate cases. The +syntax tree example is also doable, although not as elegantly. +

+

Values

@@ -1212,6 +1248,60 @@ that includes more powerful run-time support. We believe that with some effort the size of Go binaries can be reduced.

+

+Can I stop these complaints about my unused variable/import?

+ +

+The presence of an unused variable may indicate a bug, while +unused imports just slow down compilation. +Accumulate enough unused imports in your code tree and +things can get very slow. +For these reasons, Go allows neither. +

+ +

+When developing code, it's common to create these situations +temporarily and it can be annoying to have to edit them out before the +program will compile. +

+ +

+Some have asked for a compiler option to turn those checks off +or at least reduce them to warnings. +Such an option has not been added, though, +because compiler options should not affect the semantics of the +language and because the Go compiler does not report warnings, only +errors that prevent compilation. +

+ +

+There are two reasons for having no warnings. First, if it's worth +complaining about, it's worth fixing in the code. (And if it's not +worth fixing, it's not worth mentioning.) Second, having the compiler +generate warnings encourages the implementation to warn about weak +cases that can make compilation noisy, masking real errors that +should be fixed. +

+ +

+It's easy to address the situation, though. Use the blank identifier +to let unused things persist while you're developing. +

+ +
+import "unused"
+
+// This declaration marks the import as used by referencing an
+// item from the package.
+var _ = unused.Item  // TODO: Delete before committing!
+
+func main() {
+	debugData := debug.Profile()
+	_ = debugData // Used only during debugging.
+	....
+}
+
+

Performance