How to Use AI in Go with LangChainGo (Very Easy!)
Have you ever wanted to use AI in your Go (Golang) programs? It may sound hard, but with LangChainGo, it’s actually very easy. In this post, I will show you a simple example to get started. What is LangChainGo? LangChainGo is a Go library that helps you use language models (like Google AI, OpenAI, etc.) in your Go programs. You can give it a prompt (a question or a message), and it will give you an answer using AI. What We’ll Build We will write a small Go program that asks AI to explain something in simple terms. In this case, we ask: "Explain the concept of quantum entanglement in simple terms" The AI will give us an answer we can print. The Code Here is the full code: package main import ( "context" "fmt" "log" "os" "github.com/tmc/langchaingo/llms" "github.com/tmc/langchaingo/llms/googleai" ) func main() { ctx := context.Background() apiKey := os.Getenv("API_KEY") llm, err := googleai.New(ctx, googleai.WithAPIKey(apiKey)) if err != nil { log.Fatal(err) } prompt := "Explain the concept of quantum entanglement in simple terms" answer, err := llms.GenerateFromSinglePrompt(ctx, llm, prompt) if err != nil { log.Fatal(err) } fmt.Println(answer) } What This Code Does First, it sets up a context. It gets your Google AI API key from the environment (API_KEY). Then it creates a connection to the AI using the LangChainGo library. It sends a prompt to the AI. The AI gives an answer. We print the answer. Example Output When I run the code, I get something like: "Quantum entanglement is when two tiny particles are connected in a special way, so if you do something to one, the other changes instantly, no matter how far apart they are." Amazing, right? With just a few lines of code, you can use the power of AI! Final Thoughts LangChainGo makes it very easy to use AI in Go. You don’t need to be an expert in machine learning or AI. If you know some Go, you can get started right away. Let me know if you try it out or if you have any questions!

Have you ever wanted to use AI in your Go (Golang) programs? It may sound hard, but with LangChainGo, it’s actually very easy. In this post, I will show you a simple example to get started.
What is LangChainGo?
LangChainGo is a Go library that helps you use language models (like Google AI, OpenAI, etc.) in your Go programs. You can give it a prompt (a question or a message), and it will give you an answer using AI.
What We’ll Build
We will write a small Go program that asks AI to explain something in simple terms. In this case, we ask:
"Explain the concept of quantum entanglement in simple terms"
The AI will give us an answer we can print.
The Code
Here is the full code:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/googleai"
)
func main() {
ctx := context.Background()
apiKey := os.Getenv("API_KEY")
llm, err := googleai.New(ctx, googleai.WithAPIKey(apiKey))
if err != nil {
log.Fatal(err)
}
prompt := "Explain the concept of quantum entanglement in simple terms"
answer, err := llms.GenerateFromSinglePrompt(ctx, llm, prompt)
if err != nil {
log.Fatal(err)
}
fmt.Println(answer)
}
What This Code Does
- First, it sets up a context.
- It gets your Google AI API key from the environment (
API_KEY
). - Then it creates a connection to the AI using the LangChainGo library.
- It sends a prompt to the AI.
- The AI gives an answer.
- We print the answer.
Example Output
When I run the code, I get something like:
"Quantum entanglement is when two tiny particles are connected in a special way, so if you do something to one, the other changes instantly, no matter how far apart they are."
Amazing, right? With just a few lines of code, you can use the power of AI!
Final Thoughts
LangChainGo makes it very easy to use AI in Go. You don’t need to be an expert in machine learning or AI. If you know some Go, you can get started right away.
Let me know if you try it out or if you have any questions!