Explore your understanding of Go programming by answering questions focused on structs and interfaces. This quiz covers essential syntax, embedding, method association, and interface implementation in Go, making it ideal for beginners and those reviewing core concepts.
Which of the following is the correct way to declare a struct type named 'Car' with fields 'Brand' (string) and 'Year' (int) in Go?
Explanation: The correct syntax in Go to declare a struct type is 'type Car struct { Brand string; Year int }'. The second option uses C-like syntax, which is not valid in Go. The third and fourth options include misplaced colons and incorrect struct type declarations. Only the first option accurately follows Go's struct declaration rules.
When initializing a struct in Go, which of the following lets you set only certain fields by name, leaving others to their zero values?
Explanation: A named field struct literal in Go lets you specify values for some fields by name, and any fields not mentioned are set to their zero values. The positional struct literal requires you to list all fields in order. Constructor functions are not built-in in Go, and default initializer is not a Go term. Thus, 'Named field struct literal' is the valid method.
In Go, how are methods associated with a custom struct type?
Explanation: Methods in Go are defined as functions with a receiver, typically a struct, before the method name. Including methods within a struct is not supported in Go as it is in some other languages. There is no 'associate' keyword, and interface methods are not declared inside structs. Only receiver functions correctly create methods for structs.
Which statement about anonymous structs in Go is correct?
Explanation: Anonymous structs in Go are created without naming the struct type, ideal for one-off uses. A type declaration is not needed, so option two is incorrect. Anonymous structs can have fields, making option three wrong. Go fully supports anonymous structs, so option four is incorrect.
How does a type implement an interface in Go?
Explanation: In Go, a type implements an interface simply by having all the methods the interface declares. There is no requirement or mechanism for an explicit statement, extension, or inheritance as in some other languages. Hence, only the first option accurately describes Go's interface implementation.
What will happen if you declare a variable of an interface type and assign it a struct that does not implement all its methods?
Explanation: If a struct does not implement all the required methods of an interface, attempting to assign it to an interface variable will cause a compile-time error. The compiler does not generate missing methods nor does the interface variable become nil due to incomplete implementation. Thus, only the first option describes the actual behavior.
When a struct embeds another struct, what is a primary benefit in Go?
Explanation: Embedding a struct enables the outer struct to access the fields and methods of the embedded struct as if they were its own. The embedded struct does not gain capabilities from the parent, interfaces are not implemented by default, and memory allocation is not necessarily reduced. Therefore, the direct access to fields and methods is the main advantage.
Which approach allows a struct method to modify the struct's field value in Go?
Explanation: By using a pointer receiver in a method, you can modify the underlying struct's fields. A value receiver receives a copy and changes do not persist. Methods are defined outside the struct in Go, not within it. You cannot directly assign to struct fields inside an interface. Therefore, pointer receivers are the correct approach.
Given the interface 'type Speaker interface { Speak() string }', which of the following is true about assigning a struct with a 'Speak() string' method to a variable of type Speaker?
Explanation: A struct that has a method matching the interface's requirements can be assigned to an interface variable in Go. There's no need for explicit implementation or type declaration. Both pointers and values can be assigned if the receiver type matches. Structs can indeed be assigned to interfaces if they implement the necessary method.
What is the zero value of an interface in Go before it is assigned, and how is it represented?
Explanation: An unassigned interface variable in Go has the zero value nil, meaning it has neither a dynamic type nor a value. An empty struct is not related to interface zero values, and there is no concept of a null reference to a matching struct. It does not cause a runtime panic just by being unassigned.