
Go(3) Common Keywords
Part3. Common Keyword 1. For And Range 对于所有的 range 循环,Go 语言都会在编译期将原切片或者数组赋值给一个新变量 ha,在赋值的过程中就发生了拷贝,而我们又通过 len 关键字预先获取了切片的长度,所以在循环中追加新的元素也不会改变循环执行的次数,这也就解释了循环永动机一节提到的现象。 ...

Part3. Common Keyword 1. For And Range 对于所有的 range 循环,Go 语言都会在编译期将原切片或者数组赋值给一个新变量 ha,在赋值的过程中就发生了拷贝,而我们又通过 len 关键字预先获取了切片的长度,所以在循环中追加新的元素也不会改变循环执行的次数,这也就解释了循环永动机一节提到的现象。 ...

Part2. Language Basic 1. Function 1 2 3 4 type funcval struct { fn uintptr // variable-size, fn-specific data here } funcval使用二级指针,可以处理闭包(1.外部定义,内部引用的自由变量 2.脱离闭包上下文也能保留这些变量) ...

Part1. Builtin Data Structure 0. Type 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 package builtin type bool bool const ( true = 0 == 0 // Untyped bool. false = 0 != 0 // Untyped bool. ) type uint8 uint8 type uint16 uint16 type uint32 uint32 type uint64 uint64 type int8 int8 type int16 int16 type int32 int32 type int64 int64 type float32 float32 type float64 float64 type complex64 complex64 type complex128 complex128 type string string type int int type uint uint type uintptr uintptr // byte is an alias for uint8 and is equivalent to uint8 in all ways. type byte = uint8 // rune is an alias for int32 and is equivalent to int32 in all ways. type rune = int32 const iota = 0 // Untyped int. // nil is a predeclared identifier representing the zero value for a // pointer, channel, func, interface, map, or slice type. var nil Type // Type must be a pointer, channel, func, interface, map, or slice type type Type int type Type1 int type IntegerType int type FloatType float32 type ComplexType complex64 func append(slice []Type, elems ...Type) []Type func copy(dst, src []Type) int func delete(m map[Type]Type1, key Type) func len(v Type) int func cap(v Type) int func make(t Type, size ...IntegerType) Type func new(Type) *Type func complex(r, i FloatType) ComplexType func real(c ComplexType) FloatType func imag(c ComplexType) FloatType func close(c chan<- Type) func panic(v interface{}) func recover() interface{} func print(args ...Type) func println(args ...Type) type error interface { Error() string } 对于类型的定义: ...

Part0. Compilers Principles 1. Stage 编译器技术影响了计算机的体系结构,同时也受到体系结构发展的影响。 体系结构的很多现代创新都依赖于编译器能从源程序中抽取出有效利用硬件能力的机会。 ...

Design Pattern 1. Creational Patterns 1.1.Factory Method 工厂方法模式是一种创建型设计模式, 其在父类中提供一个创建对象的方法, 允许子类决定实例化对象的类型。 Usage 无法预知对象确切类别及其依赖关系。(只需要开发新的创建者子类, 然后重写其工厂方法) 希望用户能扩展软件库或框架的内部组件。 如果希望复用现有对象来节省系统资源, 而不是每次都重新创建对象。 Advantages 避免创建者和具体产品之间的紧密耦合。 单一职责原则。 将产品创建代码放在程序的单一位置, 从而使得代码更容易维护。 开闭原则。 无需更改现有客户端代码, 可以在程序中引入新的产品类型。 Disadvantages 应用工厂方法模式需要引入许多新的子类, 代码可能会因此变得更复杂。 最好的情况是将该模式引入创建者类的现有层次结构中。 Demo 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 27 28 29 30 31 32 33 34 35 public abstract class NumberFormat extends Format { private static NumberFormat getInstance(Locale desiredLocale, int choice) { LocaleProviderAdapter adapter; adapter = LocaleProviderAdapter.getAdapter(NumberFormatProvider.class, desiredLocale); NumberFormat numberFormat = getInstance(adapter, desiredLocale, choice); if (numberFormat == null) { numberFormat = getInstance(LocaleProviderAdapter.forJRE(), desiredLocale, choice); } return numberFormat; } private static NumberFormat getInstance(LocaleProviderAdapter adapter, Locale locale, int choice) { NumberFormatProvider provider = adapter.getNumberFormatProvider(); NumberFormat numberFormat = null; switch (choice) { case NUMBERSTYLE: numberFormat = provider.getNumberInstance(locale); break; case PERCENTSTYLE: numberFormat = provider.getPercentInstance(locale); break; case CURRENCYSTYLE: numberFormat = provider.getCurrencyInstance(locale); break; case INTEGERSTYLE: numberFormat = provider.getIntegerInstance(locale); break; } return numberFormat; } } 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 // Calendar 三个子类,都可以调用getInstance() -> createClendar // BuddhistCalendar // GregorianCalendar // JapaneseImperialCalendar private static Calendar createCalendar(TimeZone zone, Locale aLocale) { CalendarProvider provider = LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale) .getCalendarProvider(); if (provider != null) { try { return provider.getInstance(zone, aLocale); } catch (IllegalArgumentException iae) { // fall back to the default instantiation } } Calendar cal = null; if (aLocale.hasExtensions()) { String caltype = aLocale.getUnicodeLocaleType("ca"); if (caltype != null) { switch (caltype) { case "buddhist": cal = new BuddhistCalendar(zone, aLocale); break; case "japanese": cal = new JapaneseImperialCalendar(zone, aLocale); break; case "gregory": cal = new GregorianCalendar(zone, aLocale); break; } } } if (cal == null) { // If no known calendar type is explicitly specified, // perform the traditional way to create a Calendar: // create a BuddhistCalendar for th_TH locale, // a JapaneseImperialCalendar for ja_JP_JP locale, or // a GregorianCalendar for any other locales. // NOTE: The language, country and variant strings are interned. if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") { cal = new BuddhistCalendar(zone, aLocale); } else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja" && aLocale.getCountry() == "JP") { cal = new JapaneseImperialCalendar(zone, aLocale); } else { cal = new GregorianCalendar(zone, aLocale); } } return cal; } 1.2.Abstract Factory 抽象工厂模式是一种创建型设计模式, 它能创建一系列相关的对象, 而无需指定其具体类。 ...