Golang Basics¶

GoNB is a Go Notebook Kernel for Jupyter. Other available Jupyter kernels can be found here.

go install github.com/janpfeifer/gonb@latest && \
  go install golang.org/x/tools/cmd/goimports@latest && \
  go install golang.org/x/tools/gopls@latest

gonb --install

or:

go install github.com/janpfeifer/gonb@latest
go install golang.org/x/tools/cmd/goimports@latest
go install golang.org/x/tools/gopls@latest

echo -e "\nexport GOPATH=/root/go" >> ~/.bashrc
echo -e "\nexport PATH=\"$GOPATH/bin:$PATH\"" >> ~/.bashrc

export GOPATH="/root/go"
export PATH="$GOPATH/bin:$PATH"

~/go/bin/gonb --install
In [2]:
import "fmt"

func main() {
  // Declare and initialize a variable
  message := "Hello, Go!"

  // Print the message
  fmt.Println(message)

  // Call a function
  result := add(5, 3)
  fmt.Println("Sum:", result)
}

// A simple function to add two integers
func add(a, b int) int {
  return a + b
}
Hello, Go!
Sum: 8