First check the Go version by running the command
Create a folder
Switch to created folder
Initialize the go project by running the given command. You will get go.mod file.
Create a main.go file, use terminal or bash
touch main.go
Let’s write some basic code in main.go file.
import (
“fmt”
)
func main () {
fmt.Println(“Hello From Go”)
}
Run the code by following command
You will see this output in console
Install some packages to connect MongoDB. Here are commands
go get go.mongodb.org/mongo-driver/mongo
Revisit the main.go file to connect MongoDB. Add some code to connect MongoDB. This is how it will look after adding.
// package importing
import (
“fmt”
“log”
“context”
“go.mongodb.org/mongo-driver/mongo”
“go.mongodb.org/mongo-driver/mongo/options”
)
// mongodb connections
var collection *mongo.Collection
var ctx = context.TODO()
func init() {
clientOptions := options.Client().ApplyURI(“mongodb://localhost:27017/”)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
// check connection
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(“Connected to MongoDB!”)
collection = client.Database(“testingWithGo”).Collection(“movies”)
}
// entry point
func main () {
fmt.Println(“Hello From Go”)
}
Run the code by following command
You will see this output in console
Hello From Go
Till, Our MongoDB is connected. Now let’s add some data. We are going to add movie data. So, let’s make a movie structure.
type Movie struct {
ID string `json:”id”`
Title string `json:”title”`
}
Now write a function to add movie to database.
func addMovie(id, title string) {
movie := Movie{
ID: id,
Title: title,
}
_, err := collection.InsertOne(ctx, movie)
if err != nil {
log.Fatal(err)
}
fmt.Println(“Movie added successfully!”)
}
Call the addMovie function to main function.
This is how our code will look like after completing above steps
// package importing
import (
“fmt”
“log”
“context”
“go.mongodb.org/mongo-driver/mongo”
“go.mongodb.org/mongo-driver/mongo/options”
)
// mongodb connections
var collection *mongo.Collection
var ctx = context.TODO()
func init() {
clientOptions := options.Client().ApplyURI(“mongodb://localhost:27017/”)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
// check connection
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(“Connected to MongoDB!”)
collection = client.Database(“testingWithGo”).Collection(“movies”)
}
// movie model or structure
type Movie struct {
ID string `json:”id”`
Title string `json:”title”`
}
// to add movie to db
func addMovie(id, title string) {
movie := Movie{
ID: id,
Title: title,
}
_, err := collection.InsertOne(ctx, movie)
if err != nil {
log.Fatal(err)
}
fmt.Println(“Movie added successfully!”)
}
// entry point
func main () {
fmt.Println(“Hello From Go”)
// callig addMovie
addMovie(“1”, “Captain America”)
}
Run the code by following command
You will see this output in console
Hello From Go
Movie added successfully!
We have successfully added some data to MongoDB, you can verify in your database, which is your localhost.
That’s for this article. Thank You.