运算符与功能行为

I am reading through the following document,

https://code.google.com/p/go-wiki/wiki/GoForCPPProgrammers

and found the statement below a bit ambiguous:

Unlike in C++, new is a function, not an operator; new int is a syntax error.

In C++ we implement operators as functions, e.g. + using operator+.

So what is the exact difference of operator vs function in programming languages in general?

operators are part of c++ language syntax, in C++ you may 'overload' them as functions if you dont want the default behaviour, For complex types or user defined types , Language may not have the semantic of the operator known , So yuser can overload them with thier own implementation.

Although I still think the question is basically a duplicate of Difference between operator and function in C++?, it may be worthwhile to clarify what the difference means in the specific context you quoted.

The point there is that a function in C++ is something that has a name and possibly function arguments, and is called using this syntax:

func(arg1,arg2,...)

In other words, the name first, then a round bracket, then the comma-separated list of arguments. This is the function call syntax of C++.

Whereas an operator is used in the way described by clause 5 of the Standard. The details of the syntax vary depending on the kind of operator, e.g. there are unary operators like &, binary operators like +, * etc.; there is also the ternary conditional operator ? :, and then there are special keywords like new, delete, sizeof, some of which translate to function calls for user-defined types, but they don't use the function call syntax described above. I.e. you don't call

new(arg1,arg2,...)

but instead, you use a special "unary expression syntax" (§5.3), which implies, among other things, that there are no round brackets immediately after the keyword new (at least, not necessarily).

It's this syntactic difference that the authors talk about in the section you quoted.

The actual distinction between functions and operators depends on the programming language. In plain C, operators are a part of the language itself. One cannot add an operator, nor change the behavior of an existing operator. This is not the case with C++, where operators are resolved to functions.

From a totally different point of view, consider Haskell, where ANY (binary) function may be treated as a binary operator:

If you don't speak Haskell, but know about dot products, this example should still be fairly straight-forward. Given:

dotP :: (Double, Double) -> (Double, Double) -> Double
dotP (x1, y1) (x2, y2) = x1 * x2 + y1 * y2

Both

dotP (1,2) (3,4)

and

(1,2) `dotP` (3,4)

will give 11.

To address the quote in the Go documentation: The Go developers are simply stressing that where in C++, one would treat new as a keyword with its own syntax, one should treat new in Go as any other function.

"What is the difference between operators and functions?" Syntax. But in fact, it's purely a convention with regards to the language: in C++, + is an infix operator (and only operators can be infix), and func() would be a function. But even this isn't always true: MyClass::operator+() is a function, but it can, and usually is invoked using the operator syntax.

Other languages have different rules: in languages like Lisp, there is no real difference. One can distinguish between built-in functions vs. user defined functions, but the distinction is somewhat artificial, since you could easily extend lisp to add additional built-in functions. And there are languages which allow using the infix notation for user defined functions. And languages like Python map between them: lhs + rhs maps to the function call lhs.__add__( rhs ) (so "operators" are really just syntactic sugar).

I sum, there is not rule for programming languages in general. There are simply two different words, and each language is free to use them as it pleases, to best describe the language.

So what is the exact difference of operator vs function in programming languages in general?

It is broad. In an abstract syntax tree, operators are unary, binary or sometimes ternary nodes - binding expressions together with a certain precedence e.g. + has lower precedence than *, which in turn has lower precedence than new.

Functions are a much more abstract concept. As a primitive, they are typed subroutine entrypoints that depending on the language can be used as rvalues with lexical scope.

C++ allows to override (overload) operators with methods by means of dynamically dispatching operator evaluation to said methods. This is a language "feature" that - as the existence of this question implies - mostly confuses people and is not available in Go.