Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.88 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. type Foo struct{
  8.     Bars []Bar
  9. }
  10.  
  11. type Bar struct {
  12.     name string
  13. }
  14.  
  15. func main() {
  16.    
  17.     foo := Foo{[]Bar{
  18.             Bar{"Bar1"},
  19.             Bar{"Bar2"},
  20.             Bar{"Bar3"},
  21.             Bar{"Bar4"},
  22.             Bar{"Bar5"}}}
  23.        
  24.     fmt.Printf("foo: %v\n",foo)
  25.     fmt.Println()
  26.    
  27.     bars := foo.Bars[:]
  28.    
  29.     // Check if same mem location
  30.     fmt.Printf("foo.Bars location: %p\n", foo.Bars)
  31.     fmt.Printf("bars location: %p\n", bars)
  32.     fmt.Println()
  33.        
  34.     // In-place delete of element
  35.     for i:= len(bars)-1; i >= 0; i--{
  36.         if (bars[i].name == "Bar3"){
  37.             copy(bars[i:], bars[i+1:]) 
  38.             bars[len(bars) -1] = Bar{}
  39.             bars = bars[:len(bars) -1]
  40.             foo.Bars = append(bars)
  41.         }
  42.     }
  43.        
  44.     fmt.Printf("foo.bars: %v\n",foo.Bars)
  45.     fmt.Printf("bars: %v\n",bars)
  46.    
  47.     // Check if same mem location
  48.     fmt.Printf("foo.Bars location: %p\n", foo.Bars)
  49.     fmt.Printf("bars location: %p\n", bars)
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement