Re:Lessons learned porting 50k loc from Java to Go
by notafrog on Saturday April 06, 2019 @06:01AM
(#19590068)
I like the Go language for all of its efficiency and easy concurrency, but part of me can't help but think that they were dying to make some new syntax just for the sake of it.
Re:Lessons learned porting 50k loc from Java to Go
by pjmlp on Saturday April 06, 2019 @02:15PM
(#19592175)
That syntax was based on Oberon-2 (1991), given Robert Griesemer's presence on the team.
PROCEDURE (q : Query) GroupBy*(field: String): Query;
BEGIN
(* .... *)
END GroupBy;
Re:Lessons learned porting 50k loc from Java to Go
by masklinn on Saturday April 06, 2019 @07:11AM
(#19590228)
That's how you define a method in Go. The local to the left of the function name is the receiver (what `this` would be in Java).
Note that there are a few odd bits to Go methods: the receiver can be a value or a pointer, and if the receiver is a pointer it might be null, because method calls on concrete types are statically dispatched so
type S struct{}
func (self *S) Foo() {
fmt.Printf("%v\n", self)
}
func main() {
var s *S
s.Foo()
}
will print "<nil>".
Re:Lessons learned porting 50k loc from Java to Go
by pram on Saturday April 06, 2019 @05:26AM
(#19589999)
GroupBy is a method on Query, and it returns Query so the methods can be chained.
Re:Lessons learned porting 50k loc from Java to Go
by dewey on Saturday April 06, 2019 @05:22AM
(#19589993)
Query is the type. So it's a function that works on the type Query, takes an input parameter of type string and returns a struct of type Query.
Re:Lessons learned porting 50k loc from Java to Go
by Noumenon72 on Saturday April 06, 2019 @09:16AM
(#19590558)
Why is it "q Query"?
Re:Lessons learned porting 50k loc from Java to Go
by bbbobbb on Saturday April 06, 2019 @09:23AM
(#19590592)
That the "receiver" identifier.
You can think of it as "this" or "self" in other languages -> reference to the current "instance" within the method.
Re:Lessons learned porting 50k loc from Java to Go
by ovao on Saturday April 06, 2019 @09:23AM
(#19590589)
In Go, the type follows the identifier. To use a single letter or short sequence of letters is idiomatic Go for short-lived identifiers or ‘quintessential’ identifiers.
Re:Lessons learned porting 50k loc from Java to Go
by dewey on Saturday April 06, 2019 @09:24AM
(#19590594)
q of type Query. So in the function you can access the fields on q (Where you call the function on) via q.FieldName. If you do it with a pointer receiver (q *Query) like explained here (https://tour.golang.org/methods/4) you can directly manipulate the object, not the copy like in this example.
func (q Query) GroupBy(field string) Query {...}