Post-increment operator in function argument in Go, not possible? - go

How come, in Go (1.2.1), this works?
package main
import (
"fmt"
)
func main() {
var i = 0
for i < 10 {
fmt.Println(i)
i++
}
}
But this (with the increment operator in the function argument) doesn't?
package main
import (
"fmt"
)
func main() {
var i = 0
for i < 10 {
fmt.Println(i++)
}
}

In Go, i++ is a statement, not an expression. So you can't use its value in another expression such as a function call.
This eliminates the distinction between post-increment and pre-increment, which is a source of confusion and bugs.

Related

Is golang's native string hash function a perfect one?

I've found that function in the golang's source code and want to know whether it's truly a perfect hash function or not.
Is it the correct way to test that?
package main
import (
"fmt"
"strconv"
"unsafe"
)
//go:linkname strhash runtime.strhash
func strhash(p unsafe.Pointer, h uintptr) uintptr
const seed = 666
func main() {
m := make(map[uintptr]string)
for i := 0; i < 1000000000; i++ {
key := strconv.Itoa(i)
hash := strhash(unsafe.Pointer(&key), seed)
_, exist := m[hash]
if exist {
fmt.Println("collision")
break
}
m[hash] = key
}
fmt.Println("finish")
}
As far as I know/can tell, it is not. It uses the AES instructions to create the hash. You might want to check out something like https://github.com/cespare/mph.

Unable to declare function as variable outside main function in golang

Is working example
package main
import "fmt"
func main() {
var newFunc func(int, int) int
newFunc = func(i int, j int) int {
return (i * j)
}
fmt.Println(newFunc(10, 20))
}
But if I taken out the function declaration part, out of main function (as shown below), it fails to compile.. Is mandatory to declare it inside main() function ?
package main
import "fmt"
var newFunc func(int, int) int
newFunc = func(i int, j int) int {
return (i * j)
}
func main() {
fmt.Println(newFunc(10, 20))
}
Compile Err details as follows:
.\functionanonymous1.go:6:1: syntax error: non-declaration statement outside function body
.\functionanonymous1.go:6:15: missing function body
.\functionanonymous1.go:6:33: method has multiple receivers
.\functionanonymous1.go:7:2: syntax error: unexpected return, expecting )
.\functionanonymous1.go:8:1: syntax error: non-declaration statement outside function body
In Go, while declaring variables outside functions, every declaration must begin with a keyword (ie. var). It might be kept in that way so that the parsing is less complicated.
So outside of functions, you can't assign a value to some pre-declared variable (i.e. v = 1 ) and also can't use short variable declarations (i.e. v := 1). You may have noticed that from the error logs, syntax error: non-declaration statement outside function body.
package main
var a int
a = 1 // illegal
b := 1 // illegal
var c = 1 // legal
func main() {}
Updated code:
package main
import "fmt"
var newFunc = func(i int, j int) int {
return (i * j)
}
func main() {
fmt.Println(newFunc(10, 20))
}
Go Playground
Simply, golang doesn't allow that. A variable that declares outside function must contain var.
So you can try:
package main
import "fmt"
var newFunc func(int, int) int = func(i int, j int) int {
return (i * j)
}
func main() {
fmt.Println(newFunc(10, 20))
}

Can Go functions specify a particular array length?

Does Go allow functions to add array length constraints to the signature, or would length still require a runtime check?
For arrays it is more than possible, it is required. For slices it is impossible.
package main
import (
"fmt"
)
func main() {
d := [2]int{1, 2}
fmt.Println(sum(d))
}
func sum(data [2]int) int {
return data[0] + data[1]
}
https://play.golang.org/p/-VMxyDvwUt

input of any numerical value and sort in go1.5.2

package main
import (
"fmt"
"sort"
)
Confirm any numerical value here.
func go_input(){
var N,i,j int
var A =[]int{100}
fmt.Scanf("%d",&N)
for i:= 0; i < N; i++ {
fmt.Scanf("%d",&A[i])
}
}
Sort a value here.but,An error is given.
func i_Sort(){
sort.Sort(go_input())
fmt.Println(go_input())
}
Execute it here.
func main(){
i_Sort()
}
go 1.5.2
go_input() doesn't return a value, so you can't use it in fmt.Println
More importantly, you are creating a brand new array every time you call go_input(), maybe you want to return a value and then reuse that?
sort.Ints() is the function you want for sorting a slice anyways
With some small changes, you need something like this:
http://play.golang.org/p/MhOlRNCIwI

Why is it possible for me to redeclare a const in go?

For exmaple
package main
import "fmt"
const s string = "constant"
func main() {
const s = 0
fmt.Println(s)
}
actually prints
0
Yet I declared it as "constant" before main.
I thought you were unable to change a constant. If this is not the case, why not use other types?
It's a new constant in the scope of main. It doesn't change the one in the outer scope. Look up shadowing.
This program demonstrates it well:
package main
import "fmt"
func main() {
const a = 0
fmt.Println(a)
{
const a = 1
fmt.Println(a)
}
fmt.Println(a)
}
The output is as follows:
0
1
0

Resources