Suppose I get bored in typing System.out.println(message)
in all the places, I introduce a method in java that looks like
private void print (Object message) {
System.out.println(message);
}
and I call print (2)
& print ("hi")
in java wherever necessary .
Can the same be achieved in GoLang too ? A function something like this
func print(message ) {
fmt.Println (message)
}
When passing an argument by reference, the method gets a reference to the object. A reference to an object is the address of the object in memory. Now, the local variable within the method is referring to the same memory location as the variable within the caller.
Yes Go does accept first-class functions.
There is an understanding/debate of whether Go composite types are passed to function by reference. To be specific, Go does not support “Pass-By-Reference” semantic in any way. The reason is simple; Go does not support a reference variable as you have in other programming languages like C++.
Go does not have pass-by-reference semantics because Go does not have reference variables.
In Java we can pass objects to methods. For example, consider the following program : From the method side, a reference of type Foo with a name a is declared and it’s initially assigned to null.
Note: When an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that its corresponding argument does. That’s why we said that java is strictly pass-by-value.
If you have to reuse the object, you can pass it through method or constructor call. In such case, object can be used in multiple methods or classes. By: [email protected] On: Sun May 05 06:24:10 EDT 2013 0 77 0 Passing objects to a method indirectly makes call by reference mechanism.
Although Test is a class type created by the program, it is used in simply the same way as Java's built-in types. One of the most common uses of the object parameters involves constructors. Often, you will want to construct a new object so that it is initially the same as some existing object.
Go is a procedural programming language with many functional programming elements.
Functions are first-class types in Go, you can create variables of function type, and you can assign function values to these variables, and you can call them (the function value stored in them).
So you may simply do:
var myprint = fmt.Println
myprint("Hello, playground")
myprint(1, 3.14, "\n", time.Now())
Output of the above (try it on the Go Playground):
Hello, playground
1 3.14
2009-11-10 23:00:00 +0000 UTC m=+0.000000001
The advantage of the above myprint
variable is that it will have the same signature as fmt.Println()
, which is:
func Println(a ...interface{}) (n int, err error)
Yes, we can also create a wrapper function like:
func myprint(a ...interface{}) (n int, err error) {
return fmt.Println(a...)
}
And it will work the same as calling fmt.Println()
, but using a variable is less verbose, and it has the advantage that you can change the function value at runtime, which is very handy when writing tests (for an example, see Testing os.Exit scenarios in Go with coverage information (coveralls.io/Goveralls)).
More on the topic:
Dave Cheney: Do not fear first class functions
golangbot.com: First Class Functions
Yet another possibility is to use "dot import", but I advise against it:
import (
. "fmt"
)
func main() {
Println("Hello, playground")
}
When you use a "dot import", all exported identifiers of the imported package become accessible without having to use qualified identifiers. Quoting from Spec: Import declarations:
If an explicit period (
.
) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block and must be accessed without a qualifier.
Have you tried using interface
?
func print(obj interface{}) {
// logic
}
This way you can accept whatever you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With