Golang ѧϰժ¼£¨Ò»£©
-
ѧ¹ýC,python,»òÕßJavaÖ®ÀàÓïÑÔÔÙÀ´Ñ§Ï°golangÓ¦¸ÃÊÇÎÞѹÁ¦£¬¿´¿´Óï·¨¾ÍÄÜд¡£Óï·¨ÉϱȽÏÌØÊâµÄÈçÏ£º
-
ÉùÃ÷±äÁ¿²¢¸³ÖµÊ¹Óà £º=
a, b £º= 1, 2 //ÉùÃ÷±äÁ¿a,b£¬²¢ÇÒ¸³Öµ1,2
a = 2 //a¸³Öµ2
-
if ²»ÐèÒªÔ²À¨ºÅ£¬²¢ÇÒ¿ÉÒÔÖ´Ðбí´ïʽ.forÓï¾äÀàËÆ
if x:=1; x<2 {
}
-
String()º¯Êý<ͬjavaµÄtoString(),¶ÔÏóµÄ×Ö·û´®ÐÎʽ>ÖÐÈç¹ûÓжÔ×ÔÉíµÄ"%s"²Ù×÷£¬½«µ¼ÖÂÎÞÇîµÝ¹éµ÷Óá£ÒòΪ"%s"»áµ÷ÓÃString(),½«¶ÔÏóת»¯Îª×Ö·û´®ÐÎʽ¡£
type MyString string
func (m MyString) String() string {
return fmt.Sprintf("MyString=%s", m) // Error: will recur forever.return fmt.Sprintf("MyString=%s", string(m)) // OK: note conversion.
}
string(m)½«mת»¯ÎªÒ»¸öString¶ÔÏó£¬È»ºó¡°%s¡±ÊÇ×÷ÓÃÔÚ¸ÃString¶ÔÏóÉÏ£¬²»ÊÇm±¾Éí£¬ËùÒÔ²»»Øµ¼ÖÂÎÞÇîµÝ¹é
-
-
deferÓеãfinallyµÄζµÀ£¬µ«ÊÇÓÖÓв»Í¬
for i := 0; i < 5; i++ {defer fmt.Printf("%d ", i) }
Deferred functions are executed in LIFOºó½øÏȳö<Õ»> order, so this code will cause 4 3 2 1 0
func trace(s string) string {fmt.Println("entering:", s)return s } func un(s string) {fmt.Println("leaving:", s) } func a() {defer un(trace("a"))fmt.Println("in a") } func b() {defer un(trace("b"))fmt.Println("in b")a() } func main() {b() }
The arguments to the deferred function (which include the receiver if the function is a method) areevaluated when the defer executes, not when the call executes.
deferÓï¾äÖ´ÐÐʱ£¬Á¢¼´¼ÆËã±»deferµÄº¯ÊýËùÐèÒªµÄËùÓвÎÊý£¬¶ø²»Êǵȵ½±»deferµÄº¯ÊýÖ´ÐвżÆËã¸Ãº¯ÊýµÄ²ÎÊý¡£½á¹ûÈçÏÂ
entering: b in b entering: a in a leaving: a leaving: b
-
new and make
new(T) returns a pointer to a newly allocated zero value of type T¡£newÊʺϳõʼ»¯0Öµ¶ÔÏó£¬new(File) and &File{} are equivalent¡£new([]int) returns a pointer to a newly allocated, zeroed slice structure, that is, a pointer to a nil slice value.
make(T, args) creates slices, maps, and channels only, and it returns an initialized (not zeroed) value of type T (not *T)
-
Arrays
Arrays are values. Assigning one array to another copies all the elements. Êý×é²»ÊÇÒýÓã¬ÊÇÖµ£¬Êý×éa¸³Öµ¸øb,Ö±½Ó¿½±´³ÉÁíÒ»·Ýb£¬½á¹ûÊÇÊý×éa,b¶ÀÁ¢£¬ÄÚÈÝÏàͬ
In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it. Êý×é×÷Ϊ²ÎÊý£¬´«µÝµÄÒ²ÊÇÕû¸öÊý×éµÄÖµµÄ¿½±´£¬¶ø²»ÊÇÖ¸Õë
ÒÔÉÏÁ½µãÓëCÓïÑÔ²»Í¬£¬Ã¿´Î¿½±´Êý×é´ú¼Û°º¹ó£¬Èç¹ûÏ£ÍûÀàËÆCÓïÑÔ£¬¿ÉÒÔ´«µÝÖ¸Õë¡£
-
Slices
SlicesÐÐΪ²ÅÓëCµÄÊý×éÒ»Ö£¬ÊÇÖ¸Õë´«µÝ¡£Slices¸ü³£Óá£Slicesµ×²ãÊÇÊý×飬ֻҪ²»³¬¹ýµ×²ãÊý×éµÄÈÝÁ¿£¬SlicesµØÖ·²»±ä¡£Ò»µ©Ìí¼ÓÔªËØµ¼Ö³¬¹ýµ×²ãÊý×éµÄÈÝÁ¿£¬¾Í»áreallocatedÖØÐ·ÖÅ䡣ĬÈϵײãÊý×éÈÝÁ¿Îªcap(slice)¡£
-
ÌØÊâÓï·¨...(Èý¸öµãµÄÊ¡ÂÔºÅ)
x := []int{1,2,3}
y := []int{4,5,6}
x = append(x, y...)//È¥µô...±¨´í£¬ÒòΪy²»ÊÇintÀàÐÍ
fmt.Println(x)//ÓеãÀàËÆPython¶ÔÓÚ×Öµä**dict
-
initº¯Êý
ÿ¸öÔ´´úÂëÎļþ¶¼¿ÉÒÔÓÐÒ»¸öinitº¯Êý£¬ÔÚ¸ÃÔ´´úÂëÎļþµÄimportÓï¾äÖ´ÐÐÍêºó£¬²¢ÇÒ±äÁ¿µ÷ÓÃÆäinitializersºóÖ´ÐÐinitº¯Êý¡£Ò»°ãÓÃÓÚ¼ì²é¸ÃÎļþÖеĸ÷ÖÖ״̬ÊÇ·ñÕýÈ·
-
import for side effect
ÓÐʱºòimportÒ»¸ölibrary£¬²¢²»ÊÇΪÁËʹÓÃij¸öº¯Êý»òÕßÀ࣬½ö½öÖ»ÊÇÏ£Íû¸ÃlibraryµÄinitº¯ÊýÖ´ÐÐÒ»´Î£¬×öÒ»ÏÂ×¼±¸¹¤×÷£º
import _ "net/http/pprof"//during its init function, the net/http/pprof package registers HTTP handlers that provide debugging information
-
Channel
¹ÜµÀ³£ÓÃÓÚgoroutineÖ®¼äµÄͨÐÅ£¬Í¬²½£»Go²»ÍƼöʹÓù²ÏíÄڴ棬»¥³âÔªµÈ·½Ê½Í¨ÐÅ¡£¹ÜµÀÓÃÓÚgoroutineͬ²½Àý×Ó£º
c := make(chan int) // ³õʼ»¯Ò»¸ö²»´ø»º´æµÄ¹ÜµÀ go func() {list.Sort()c <- 1 // Send a signal; value does not matter. }() //ÐÂÆðÒ»¸ögoroutine£¬È¥×öÅÅÐò£¬ÅÅÐòÍêºóÏò¹ÜµÀдÈëÊý¾Ý doSomethingForAWhile() <-c // ×èÈûµÈ´ý¹ÜµÀÓÐÊý¾ÝдÈ룬¼´µÈ´ýÉÏÃægoroutineÅÅÐòÍê³É
¹ÜµÀÓÃ×÷semaphore£¨ÐźÅÁ¿£©,±ÈÈçÏÞÁ÷£¬Àý×Ó£º
var sem = make(chan int, 100) func Serve(queue chan *Request) { for req := range queue {tmp := req // Create new instance of req for every goroutine.sem <- 1 //ÐźÅÁ¿ÂúʱдÈë×èÈûgo func() {process(tmp)<-sem //´¦ÀíÍêÒ»¸öÇëÇóÊÍ·ÅÒ»´ÎÐźÅÁ¿}() } }
ÓÉÓڹܵÀsem·ÖÅäÁËbuffer£¬sizeΪ100.ËùÒÔǰ100¸öÇëÇ󣬿ÉÒÔ²»×èÈûÁ¢¼´µÃµ½Ö´ÐС£ºóÃæµÄÇëÇó±ØÐëÅŶӵȴýÇ°Ãæ100¸öÇëÇóµ±ÖУ¬ÓÐÇëÇó´¦ÀíÍê±Ï(¹ÜµÀÖÐÓÐÊý¾Ý±»¶Á³ö)£¬²ÅÄÜдÈë¹ÜµÀ£¬µÃµ½´¦Àí¡£ËùÒÔ£¬Õâ¸öÀý×Ó£¬¹ÜµÀÆðµ½ÏÞÁ÷×÷Ó㺲¢Ðд¦ÀíµÄÇëÇóÊý²»µÃ³¬¹ý100
-
panic recover
GoµÄÒì³£´¦Àí»úÖÆ,ͬtry...except...
Because recover always returns nil unless called directly from a deferred function, deferred code can call library routines that themselves use panic and recover without failing.
func safelyDo(work *Work) { defer func() {if err := recover(); err != nil {log.Println("work failed:", err)} }() do(work) }
doº¯ÊýÀï³öÁËpanicÒì³£,Ö´ÐÐdeferredº¯Êýfunc,funcº¯ÊýÀµ÷ÓÃrecover²¶»ñÒì³£¡£