@kjk1/

gobook function variable

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.10834802561005752","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"CnBhY2thZ2UgbWFpbgoKaW1wb3J0ICJmbXQiCgovLyA6c2hvdyBzdGFydAovLyBpbnRPcCBpcyBhIHZhcmlhYmxlIHdob3NlIHR5cGUgaXMgZnVuY3Rpb24gdGhhdCB0YWtlcwovLyAyIGludGVnZXJzIGFzIGFyZ3VtZW50cyBhbmQgcmV0dXJucyBhbiBpbnRlZ2VyCnZhciBpbnRPcCBmdW5jKGludCwgaW50KSBpbnQKCmZ1bmMgaW50QWRkKGEsIGIgaW50KSBpbnQgewoJcmV0dXJuIGEgKyBiCn0KCmZ1bmMgbWFpbigpIHsKCWludE9wID0gaW50QWRkCglmbXQuUHJpbnRmKCJpbnRPcCgyLCAzKSA9ICVkXG4iLCBpbnRPcCgyLCAzKSkKCgkvLyB3ZSBjYW4gYXNzaWduIGxpdGVyYWwgZnVuY3Rpb25zIGFzIHdlbGwKCWludE9wID0gZnVuYyhhLCBiIGludCkgaW50IHsKCQlyZXR1cm4gYSAqIGIKCX0KCWZtdC5QcmludGYoImludE9wKDIsIDMpID0gJWRcbiIsIGludE9wKDIsIDMpKQp9CgovLyA6c2hvdyBlbmQ="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import "fmt"

// :show start
// intOp is a variable whose type is function that takes
// 2 integers as arguments and returns an integer
var intOp func(int, int) int

func intAdd(a, b int) int {
	return a + b
}

func main() {
	intOp = intAdd
	fmt.Printf("intOp(2, 3) = %d\n", intOp(2, 3))

	// we can assign literal functions as well
	intOp = func(a, b int) int {
		return a * b
	}
	fmt.Printf("intOp(2, 3) = %d\n", intOp(2, 3))
}

// :show end