New function assignment syntax

in view of the fact that more than one person misunderstood I meant, I declare that this article is not a question, this is a proposal. In fact, my article title has already explained everything. misunderstand people do not read the title?

I want rename the function:

func sum(x, y int) int { return x + y }
var myfunc = sum
// func myfunc(x, y int) int { return sum(x, y) }

it works but not good enough.
if we can change var to func:

func sum(x, y int) int { return x + y }
func myfunc = sum // or: func myfunc sum ?

don’t use type derivation:

func sum(x, y int) int { return x + y }
func myfunc(x, y int) int = sum // or: func myfunc(int,int) int = sum ?
// var myfunc func(int,int) int = sum

the right is a method:

type mytype struct{}
func (_ mytype) sum(x, y int) int { return x + y }
var myobject = new(mytype)
func myfunc = myobject.sum
// var myfunc = myobject.sum

if the left is a method, I don’t know what it should be.

The difference is that the var syntax can assign multiple times, and func can only assign once.

Maybe I misunderstand. What is wrong with this reassigning function name.

package main

import (
	"fmt"
)

func func1() {
	fmt.Println("Hello, world!")
}


func func2() {
	fmt.Println("blah, world!")
}
func main() {
	f := func1
	f()
	f = func2
	f()

}

your style of writing and my style of writing are not same usage scene. When using the local variable short assignment syntax, the document can’t generate, and the renamed function can’t use as public API.

I don’t understand the problem.

func sum(x, y int) int { return x + y }
var myfunc = sum

works fine, and you have given the function a new name and can call it:

fmt.Println(myfunc(1,2)) // works

I know that can work, I am just inventing a new syntax. The new syntax let godoc see it’s a function.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.