//===
xxx assignment operator :=
--> := is shortening for declaration and initialization
--> assignment operator is =
//=== http://stackoverflow.com/questions/16521472/assignment-operator-in-go-language
wondering why use
name := "John"
instead of
name = "John"
-->
:= is not the assignment operator,
it serves both as a declaration and as initialization,
"just syntactic sugar"
foo := "bar"
is equivalent to
var foo = "bar"
*** Go is statically typed, so you have to declare variables.
//===
https://tour.golang.org/basics/1
---
https://tour.golang.org/basics/17
* the package name is the same as the last element of the import path.
[ex]
import "math/rand"
the files under "math/rand" folder begin with the statement
package rand
* In Go, a name is exported if it begins with a capital letter.
Foo is an exported name, as is FOO. The name foo is not exported.
[ex]
import "math"
math.Pi is exported and can be used outside "math" package
* function add takes two parameters of type int and return int
func add(x int, y int) int {
return x + y
}
* Inside a function, the := can be used in place of a var declaration with implicit type.
but
Outside a function, ... the := is not available.
[ex]
package main
import "fmt"
x,y := 3.3, 4.4 // --> error
func main() {
var i, j int = 1, 2
k := 3
c, python, java := true, false, "no!"
fmt.Println(i, j, k, c, python, java)
}
* shorten "x int, y int" to "x, y int" in a paramter list or a statement
func add(x, y int) int {
return x + y
}
or
var x,y int = 1,2
or
x,y := 1,2
[ex]
var c, python, java = true, false, "no!" //type inference from values
xxx var t int, s string = 1, "i am a pig"
-->
var t , s = 1, "i am a pig"
t,s:= 1, "i am a pig"
var t int ; s := "i am a pig"
* anonymous return/implicit return/naked return
A return statement without arguments returns the current values of the results.
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
//=== Go's basic types are
bool, string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
// represents a Unicode code point
float32 float64
complex64 complex128
//=== Type conversion
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
* In Go assignment between items of different type requires an explicit conversion
//=== %T , type inference
fmt.Printf("v is of type %T\n", v)
//===
Constants can be character, string, bool, or numeric values.
Constants cannot be declared using the := syntax.
An untyped constant takes the type needed by its context.
const (
Big = 1 << 100
Small = Big >> 99
)
func main()
{
t:=1
fmt.Println(needFloat(t)) // --> error , need Explicit type conversion
fmt.Println(needFloat(Small))
fmt.Println(needFloat(Big))
}
//=== http://blog.golang.org/gos-declaration-syntax
//=== C
c pointer function
int (*fp)(int a, int b);
Here, fp is a pointer to a function which receives two int parameters and return int.
if one of fp's arguments is itself a function -->
int (*fp)(int (*ff)(int x, int y), int b)
* we can leave out the name of the parameters when we declare a function, so main can be declared
int main(int argc, char *argv[])
--> int main(int, char *[])
drop the name from the middle of its declaration to construct its type.
* what happens to fp's declaration if parameters is unnamed:
int (*fp)(int (*)(int, int), int)
int (*(*fp)(int (*)(int, int), int))(int, int)
* Because C use the same type and declaration syntax, it can be difficult
to parse expressions with types in the middle.
--> C casts always parenthesize the type, as in (int)M_PI
//=== Go
* ?the real main function in Go takes no arguments?
* Here's a declaration of a function variable (analogous to a function pointer in C):
f func(func(int,int) int, int) int
Or if f returns a function:
f func(func(int,int) int, int) func(int, int) int
* Go's type syntax puts the brackets on the left of the type but the expression syntax puts them on the right of the expression:
var a []int
x = a[1]
* Go's pointer syntax is still like the familiar C form, i.e.
var p *int
x = *p
not
x= p*
* Overall, we believe Go's type syntax is easier to understand than C's, especially when things get complicated.
* Go's declarations read left to right.
It's been pointed out that C's read in a spiral! See The "Clockwise/Spiral Rule" by David Anderson.
http://c-faq.com/decl/spiral.anderson.html
2015年3月27日 星期五
golang tour basics digest
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言