Provide potentional nil variable to function in GOLANG [closed] - go

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 want to call a function with a int variable that can have a value or can be nil. In the main function i'm doing this call.
doDomething(10) // does not work?
doDomething(nil) // works
func doSomething(parent_id *int) {
fmt.Print(parent_id)
}
I'm getting the following error:
cannot use a (type int) as type *int in argument to doSomething
I use the *int pointer, therefore the nil works but not if it is a value. Then I'm getting the type mismatch.

doSomething expects a *intparameter. 10 is of type int, so this is he source of the error.
This is a valid usage:
i := 10
doDomething(&i)
This is how doSomething should look like:
func doSomething(parent_id *int) {
if parent_id == nil{
//nil
fmt.Println("nil")
return
}
//valid int
fmt.Printf("%d", *parent_id)
}

Related

can you use a primitive or inbuild data types as a method in golang [closed]

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)
}

How can I assign a function to a variable without executing it? [closed]

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 2 years ago.
Improve this question
When I try to assign a map key to a function it executes the function. How can I do this without executing the function?
var myslice []int
myslice = append(myslice, 1)
func RemoveIndex(s []string, index int) []string {
return append(s[:index], s[index+1:]...)
}
a["removeIndex"] = removeIndex(myslice, 0)
Just assign the function to the map[string]func([]string,int)[]string's key
func RemoveIndex(s []string, index int) []string {
return append(s[:index], s[index+1:]...)
}
a["removeIndex"] = RemoveIndex
rifn := a["removeIndex"]
rifn(myslice, 0)

Why are errors nillable in Go? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
An error in Go can be nil. The following compiles:
var err error
err = nil
err = errors.New("hello")
Yet errors are values, and value types are not nullable in Go.
Looking at error, it is an ordinary interface:
type error interface {
Error() string
}
implemented by this struct:
type errorString struct {
s string
}
No pointers here. There is a method that takes a pointer receiver,
func (e *errorString) Error() string {
return e.s
}
Yet that doesn't explain why error behaves as a pointer rather than a value.
errors.New() does return a pointer,
func New(text string) error {
return &errorString{text}
}
which makes my third code line above more puzzling -- we are assigning the result of New to a value variable.
How does this work?
I think you may be conflating some concepts. Yes, "errors are values", in that they are not exceptions that can be thrown, but rather they are just regular values returned from a function like any other return value. The Go quality "errors are values" has nothing to do with value vs reference semantics.
The built-in type error is an interface, and all interface values are nilable, so error is nilable.

How to find if type is float64 [closed]

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 3 years ago.
Improve this question
I am trying to find if a variable is of type float64:
package main
import ("fmt")
func main() {
myvar := 12.34
if myvar.(type) == float64 {
fmt.Println("Type is float64.")
}
}
However, it is not working and giving following error:
./rnFindType.go:6:10: use of .(type) outside type switch
./rnFindType.go:6:21: type float64 is not an expression
What is the problem and how can it be solved?
You know that myvar is a float64 because the variable is declared with the concrete type float64.
If myvar is an interface type, then you can use a type assertion to determine if the concrete value is some type.
var myvar interface{} = 12.34
if _, ok := myvar.(float64); ok {
fmt.Println("Type is float64.")
}
Try this program at https://play.golang.org/p/n5ftbp5V2Sx

Calling a function that takes an interface as a parameter in Go [closed]

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 8 years ago.
Improve this question
How do you construct an interface as a parameter to a function?
type blahinterface interface {
method1()
method2()
method3()
}
func blah (i blahinterface) {
}
blah(?) < what goes in here
Actually, if you try to put anything "in here", the compiler will tell you precisely what is missing:
type S struct{}
func main() {
fmt.Println("Hello, playground")
s := &S{}
blah(s)
}
A go build on this example would tell you:
prog.go:20: cannot use s (type *S) as type blahinterface in argument to blah:
*S does not implement blahinterface (missing method1 method)
[process exited with non-zero status]
But with:
func (s *S) method1(){}
func (s *S) method2(){}
func (s *S) method3(){}
The program do compile just fine.
So even without reading about interfaces, you are guided and can guess what is missing.

Resources