# GoBGP as a Go Native BGP library This page explains how to use GoBGP as a Go Native BGP library. ## Contents - [Basic Example](#basic) ## Basic Example ```go package main import ( "fmt" log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" gobgp "github.com/osrg/gobgp/server" "github.com/osrg/gobgp/table" "time" ) func main() { log.SetLevel(log.DebugLevel) s := gobgp.NewBgpServer() go s.Serve() // start grpc api server. this is not mandatory // but you will be able to use `gobgp` cmd with this. g := gobgp.NewGrpcServer(s, ":50051", s.GrpcReqCh) go g.Serve() // global configuration b := &config.BgpConfigSet{ Global: config.Global{ Config: config.GlobalConfig{ As: 65000, RouterId: "10.0.255.254", Port: -1, // gobgp won't listen on tcp:179 }, }, } if err := config.SetDefaultConfigValues(nil, b); err != nil { log.Fatal(err) } if err := s.Start(&b.Global); err != nil { log.Fatal(err) } // neighbor configuration if err := s.AddNeighbor(&config.Neighbor{ Config: config.NeighborConfig{ NeighborAddress: "10.0.255.1", PeerAs: 65001, }, Timers: config.Timers{ Config: config.TimersConfig{ HoldTime: 90, KeepaliveInterval: 30, }, }, AfiSafis: []config.AfiSafi{ config.AfiSafi{ Config: config.AfiSafiConfig{ AfiSafiName: config.AFI_SAFI_TYPE_IPV4_UNICAST, }, }, }, }); err != nil { log.Fatal(err) } // add routes attrs := []bgp.PathAttributeInterface{ bgp.NewPathAttributeOrigin(0), bgp.NewPathAttributeNextHop("10.0.255.254"), bgp.NewPathAttributeAsPath([]bgp.AsPathParamInterface{bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint32{4000, 400000, 300000, 40001})}), } if _, err := s.AddPath("", []*table.Path{table.NewPath(nil, bgp.NewIPAddrPrefix(24, "10.0.0.0"), false, attrs, time.Now(), false)}); err != nil { log.Fatal(err) } // monitor new routes w := s.Watch(gobgp.WatchBestPath()) for { select { case ev := <-w.Event(): switch msg := ev.(type) { case *gobgp.WatchEventBestPath: for _, path := range msg.PathList { // do something useful fmt.Println(path) } } } } } ```