Methods
Function declaration
Java
int multiply(int a, int b) {
return a*b;
}
GoLang
func multiply(a, b int) int {
return a*b
}
Multiple results
Java
class Person {
String name;
int age;
Person(String name, String age) {
this.name = name;
this.age = age;
}
}
Person getNameAndAge() {
return new Person("John", 18)
}
Person person = getNameAndAge();
var name = person.name;
var age = person.age;
GoLang
func getNameAndAge() (string, int){
return "John", 18
}
var name, age = getNameAndAge()
Error handling
Java
try {
int i = Integer.parseInt("96");
} catch (NumberFormatException e) {
System.out.err("couldn't convert: " + e.getMessage());
return;
}
}
GoLang
i, err := strconv.Atoi("96")
if err != nil {
fmt.Printf("couldn't convert: %v", err)
return
}