Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a custom type named ProtectedCustomType and I don't want the variables within that to be set or get directly by the caller, rather want a Getter / Setter methods to do that.
Below is my ProtectedCustomType
package custom
import "fmt"
type ProtectedCustomType struct {
name string
age string
phoneNumber int
}
func (pct *ProtectedCustomType) SetAge (age string) {
pct.age=age
fmt.Println(pct.age)
}
func (pct *ProtectedCustomType) GetAge () string {
return pct.age
}
And here is my main function
package main
import (
"fmt"
"./custom"
)
var print =fmt.Println
func structCheck2() {
pct := custom.ProtectedCustomType{}
pct.SetAge("23")
age:=pct.GetAge
print (age)
}
func main() {
structCheck2()
}
I am expecting it to print 23, but it is printing as 0x48b950
This (your code) takes the pct instance's GetAge method and stores it in a variable:
age:=pct.GetAge
This calls the GetAge method and stores its return value in a variable:
age:=pct.GetAge()
Consider taking the Tour of Go and reading the Go Specification to get a basic understanding of Go syntax.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I want to include different structure pointer fields in the map as shown below. (Of course the code below doesnt work)
type StructA struct {
}
type StructB struct {
}
mymap := map[string]*struct{}{
"StructA": StructA,
"StructB": StructB,
}
As #icza said the element type of a map must be a specific type. But this could be an interface that can store an object of different types. The type any (an alias for interface{} is in some ways like a pointer (though it also stores type info), so you could just do:
mymap := map[string]inteface{}{
"StructA": StructA{},
"StructB": StructB{},
}
To be a little safer you can restrict the types that can be added to the map to just the two structs. To do this you need an interface which specifies a function that both the struct types implement.
type (
Common interface{ ImplementsCommon() }
A struct{}
B struct{}
)
func (A) ImplementsCommon() {}
func (B) ImplementsCommon() {}
mymap := map[string]Common{
"A": A{},
"B": B{},
}
Try it on the Go Playground
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm a beginner to Golang coming from JavaScript land. Is there a way to join one Golang struct into another?
Example:
type SimpleInfo struct {
name string,
age int,
}
type ComplexInfo struct {
SimpleInfo,
address string,
salary int,
}
Ideally, the intention is to make ComplexInfo look like this:
{
name string,
age int,
address string,
salary int,
}
You are on the right road, don't use commas
import (
"fmt"
)
type SimpleInfo struct {
name string
age int
}
type ComplexInfo struct {
SimpleInfo
address string
salary int
}
func main() {
fa:=ComplexInfo{}
fa.name="frank"
fa.salary=1000000
fmt.Println(fa.name, fa.salary)
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i wanna know if we are able to use inbuild data types as a method for a func in golang, cause whenever I use it as such, it shows an error
You can define methods on built-in types by first wrapping them with your own types, like this:
type MyInteger int
func (my MyInteger) Tell() {
fmt.Println("I'm MyInteger with value", my)
}
func main() {
var my MyInteger = 42
my.Tell()
}
You can try this on the Go Playground, it will print:
I'm MyInteger with value 42
This can be useful if you want to make a built-in type implement an interface. For example, here's how MyInteger would implement the fmt.Stringer interface:
type MyInteger int
func (my MyInteger) String() string {
return "MyInteger " + strconv.Itoa(int(my))
}
func main() {
var my MyInteger = 42
fmt.Println(my)
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Is it possible to have multiple receivers on a single function? In other words, a single function can belong to two or more structs?
Say I have
type Struct1 struct {
foo.Client
}
func CreateClient() struct1 {
return struct1{
ClientID: cId,
// ...
}
}
func (s *Struct1) MyFunc( // ... ) {}
But I also want to be able to associate MyFunc with another struct (different package):
type Struct2 struct {
lgr log.logger
}
func NewStruct2 (l *log.logger) (*Struct2, err) {
return &Struct2{mylog: *l}, nil
}
So what I want to actually have is:
func (s1 *Struct1, s2 *Struct2) MyFunc( // ... ) {}
"Is it possible to have multiple receivers on a single function?" -- It is not possible.
https://golang.org/ref/spec#Method_declarations
The receiver is specified via an extra parameter section preceding the
method name. That parameter section must declare a single non-variadic
parameter, the receiver.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to define additional methods on a basic map https://play.golang.org/p/3BKgxVJIjP1:
type Typ struct {
config string
}
type typeRegistry = map[string]Typ
func (r typeRegistry) Add(name string) {
typ := Typ{
config: "config",
}
r[name] = typ
}
Doing so fails:
invalid receiver type map[string]Typ (map[string]Typ is not a defined type)
Before refactoring the approach was similar but with a func instead of Typ:
type typeRegistry map[string]func()
func (r typeRegistry) Add(name string, factory func()) {
r[name] = factory
}
This version worked. Where is the difference in defining additional methods on map-type receivers?
type typeRegistry = map[string]Typ
Is a type alias. You cannot define methods on aliases (only on the original type--but in this case map[string]Typ cannot have methods on it, so you're out of luck).
What you probably want is to create a custom type, not an alias:
type typeRegistry map[string]Typ
Then your methods will work.