Source file test/fixedbugs/issue19359.go

     1  // run
     2  
     3  // Copyright 2017 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  import "fmt"
    10  
    11  func set(m map[interface{}]interface{}, key interface{}) (err error) {
    12  	defer func() {
    13  		if r := recover(); r != nil {
    14  			err = fmt.Errorf("set failed: %v", r)
    15  		}
    16  	}()
    17  	m[key] = nil
    18  	return nil
    19  }
    20  
    21  func del(m map[interface{}]interface{}, key interface{}) (err error) {
    22  	defer func() {
    23  		if r := recover(); r != nil {
    24  			err = fmt.Errorf("del failed: %v", r)
    25  		}
    26  	}()
    27  	delete(m, key)
    28  	return nil
    29  }
    30  
    31  func addInt(m map[interface{}]int, key interface{}) (err error) {
    32  	defer func() {
    33  		if r := recover(); r != nil {
    34  			err = fmt.Errorf("addInt failed: %v", r)
    35  		}
    36  	}()
    37  	m[key] += 2018
    38  	return nil
    39  }
    40  
    41  func addStr(m map[interface{}]string, key interface{}) (err error) {
    42  	defer func() {
    43  		if r := recover(); r != nil {
    44  			err = fmt.Errorf("addStr failed: %v", r)
    45  		}
    46  	}()
    47  	m[key] += "hello, go"
    48  	return nil
    49  }
    50  
    51  func appendInt(m map[interface{}][]int, key interface{}) (err error) {
    52  	defer func() {
    53  		if r := recover(); r != nil {
    54  			err = fmt.Errorf("appendInt failed: %v", r)
    55  		}
    56  	}()
    57  	m[key] = append(m[key], 2018)
    58  	return nil
    59  }
    60  
    61  func appendStr(m map[interface{}][]string, key interface{}) (err error) {
    62  	defer func() {
    63  		if r := recover(); r != nil {
    64  			err = fmt.Errorf("addStr failed: %v", r)
    65  		}
    66  	}()
    67  	m[key] = append(m[key], "hello, go")
    68  	return nil
    69  }
    70  
    71  func main() {
    72  	m := make(map[interface{}]interface{})
    73  	set(m, []int{1, 2, 3})
    74  	set(m, "abc") // used to throw
    75  	del(m, []int{1, 2, 3})
    76  	del(m, "abc") // used to throw
    77  
    78  	mi := make(map[interface{}]int)
    79  	addInt(mi, []int{1, 2, 3})
    80  	addInt(mi, "abc") // used to throw
    81  
    82  	ms := make(map[interface{}]string)
    83  	addStr(ms, []int{1, 2, 3})
    84  	addStr(ms, "abc") // used to throw
    85  
    86  	mia := make(map[interface{}][]int)
    87  	appendInt(mia, []int{1, 2, 3})
    88  
    89  	msa := make(map[interface{}][]string)
    90  	appendStr(msa, "abc") // used to throw
    91  }
    92  

View as plain text