0

I was reading the source code of package math and I don't understand how the Max function is defined.

Namely, there seems to be a function definition for Max that somehow hangs, for lack of a better word.

Also, there seems to be another non-exported function named max with the same signature as Max, but this time, its implementation is present.

Why doesn't the compiler complain about function Max in eg

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Max(1.1, 2.2)=", math.Max(1.1, 2.2))
}

whereas if one does something like

package main

import (
    "fmt"
)

func A() bool

func a() bool { return true }

func main() {
    fmt.Println(A())
}

in the Playground, the compiler produces./prog.go:7:6: missing function body?

How is it possible to call math.Max and obtain a result, when the body of the function, in its source file, is missing?

Edit

I think it's not a duplicate of the cited questions because this question is predominantly interested about math.Max and its implementation and only circumstantially touches upon the issue addresed in the linked questions which is function definitions without a body. Also, it might prove useful to a future search about Max to read the comments about the implementation of Max in assembly outside of Go.

joka
  • 169
  • 8
  • 1
    There seem to be platform specific implementations for those functions, in assembly: https://golang.org/src/math/dim_amd64.s . Also relevant: https://golang.org/doc/asm – Felix Kling Dec 29 '20 at 20:15
  • @FelixKling how come the compiler doesn't complain about the `Max` function missing its body? Or is it the case that the source file containing its signature is a mock-up file for display purposes only? – joka Dec 29 '20 at 20:18
  • I guss this will answer your question: [What does a function without body mean?](https://stackoverflow.com/q/14938960/218196). Maybe even a duplicate? – Felix Kling Dec 29 '20 at 20:19
  • @FelixKling practically, yes; but I think there is no answer there for the location of the implementation of `Max` that you provided in your comment above so I guess if someone in the future is interested about that, they might find your comment to my question useful... – joka Dec 29 '20 at 20:27
  • 1
    If you're asking because you want to know what the behavior of the Max function is, look at the lowercase max function, as that is the default and all platform specific implementations are supposed to be compliant with it. If you want to see the assembly implementations for different platforms, you can search within the math package source files. This file contains the amd64 source https://github.com/golang/go/blob/0e85fd7561de869add933801c531bf25dee9561c/src/math/dim_amd64.s – Hymns For Disco Dec 30 '20 at 07:26

0 Answers0