// Divide function with try-catch-finally publicstaticintdivide(int a, int b) { try { if (b == 0) { thrownewArithmeticException("division by zero"); } return a / b; } catch (ArithmeticException e) { System.out.println("Error: " + e.getMessage()); return0; // or handle the error in some way } finally { System.out.println("Division operation completed."); } }
//slice会报错: Invalid map key type: comparison operators == and != must be fully defined for the key type
slice不能作为map key的原因是因为Go没有实现相等性(equality)操作符,这是切片的相等性在定义上并不明确,存在多个考虑因素,比如浅层比较 vs. 深层比较、指针比较 vs. 值比较、如何处理递归类型等等。并且Slice还有一个动态伸缩的特性,它的映射也会丢失。因为键不会具有和之前相同的hashCode。
//slice底层结构 type slice struct { array unsafe.Pointer // type Pointer *ArbitraryType lenint capint } //map底层结构 // A header for a Go map. type hmap struct { // Note: the format of the hmap is also encoded in cmd/compile/internal/reflectdata/reflect.go. // Make sure this stays in sync with the compiler's definition. count int// # live cells == size of map. Must be first (used by len() builtin) flags uint8 B uint8// log_2 of # of buckets (can hold up to loadFactor * 2^B items) noverflow uint16// approximate number of overflow buckets; see incrnoverflow for details hash0 uint32// hash seed
buckets unsafe.Pointer // array of 2^B Buckets. may be nil if count==0. oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing nevacuate uintptr// progress counter for evacuation (buckets less than this have been evacuated)
extra *mapextra // optional fields }
这里通过几个例子来看下:
1 2 3 4 5
funcmodifyInt(x int) { x = 42 }
之前定义方法的时候提到过使用值或者指针的方法来定义:
1 2
func(s *MyStruct) pointerMethod() { } // method on pointer func(s MyStruct) valueMethod() { } // method on value
// The new built-in function allocates memory. The first argument is a type, // not a value, and the value returned is a pointer to a newly // allocated zero value of that type. funcnew(Type) *Type
// The make built-in function allocates and initializes an object of type // slice, map, or chan (only). Like new, the first argument is a type, not a // value. Unlike new, make's return type is the same as the type of its // argument, not a pointer to it. The specification of the result depends on // the type: // // Slice: The size specifies the length. The capacity of the slice is // equal to its length. A second integer argument may be provided to // specify a different capacity; it must be no smaller than the // length. For example, make([]int, 0, 10) allocates an underlying array // of size 10 and returns a slice of length 0 and capacity 10 that is // backed by this underlying array. // Map: An empty map is allocated with enough space to hold the // specified number of elements. The size may be omitted, in which case // a small starting size is allocated. // Channel: The channel's buffer is initialized with the specified // buffer capacity. If zero, or the size is omitted, the channel is // unbuffered. funcmake(t Type, size ...IntegerType) Type
new不会初始化内存,而是将其清零( only zeros),换句话来说就是new(T)为T类型分配归零内存并返回它的地址,即 *T 类型的值。