Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to Java's passing of Object as method parameter in GoLang

Tags:

go

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)
}
like image 635
Arun Avatar asked Oct 17 '18 09:10

Arun


People also ask

When an object is passed as an argument to a method?

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.

Can I pass function as parameter in Golang?

Yes Go does accept first-class functions.

Is Golang pass by value or pass by reference?

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++.

Are objects passed by reference in Golang?

Go does not have pass-by-reference semantics because Go does not have reference variables.

How to pass an object to a method in Java?

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.

Is Java pass by value or call-by-reference?

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.

How to pass an object from one class to another?

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.

What is an object parameter in Java?

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.


2 Answers

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.

like image 82
icza Avatar answered Sep 29 '22 11:09

icza


Have you tried using interface ?

func print(obj interface{}) {
 // logic
}

This way you can accept whatever you want.

like image 44
Tomasz Bawor Avatar answered Sep 29 '22 10:09

Tomasz Bawor