[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-golang-the-series-ep146-prompt-engineering-for-gophers-all--*":3,"academy-blog-translations-k7elykum3z2xaw7":79},{"data":4,"page":65,"perPage":65,"totalItems":65,"totalPages":65},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":73,"keywords":74,"locale":47,"published_at":75,"scheduled_at":13,"school_blog":69,"short_description":76,"status":67,"title":77,"updated":78,"updated_by":13,"slug":70,"views":72},"Advanced Prompt Engineering techniques within Go source code for AI application development","sclblg987654321","school_blog_translations","\u003Cp>Welcome to EP.146! After experimenting with running models on both the Cloud and Local LLMs, many of you have likely encountered the same headache: \u003Cem>\"Why isn't the AI following the brief?\"\u003C\u002Fem> or \u003Cem>\"Why is it so difficult to parse this output into my Go code?\"\u003C\u002Fem>\u003C\u002Fp>\u003Cp>Today, we’re diving into \u003Cstrong>Prompt Engineering\u003C\u002Fstrong>. But we aren’t talking about it from the perspective of a casual user chatting for fun. We’re looking at it as Gophers—developers who need to embed prompts within source code to control AI precisely and ensure seamless integration with our backend systems.\u003C\u002Fp>\u003Ch2>Structure is Everything: Constants and Templates\u003C\u002Fh2>\u003Cp>Hardcoding long prompts directly into your functions not only makes your code look messy but also makes editing or A\u002FB testing your prompts a nightmare. Professional Gophers manage their prompts by keeping them modular and organized.\u003C\u002Fp>\u003Ch3>A. Using Constants (For Static Prompts)\u003C\u002Fh3>\u003Cp>If your prompt is fixed and doesn't change based on variables, the best practice is to use Raw String Literals (backticks) and store them in a constant outside your function.\u003C\u002Fp>\u003Cp>Go\u003C\u002Fp>\u003Cpre>\u003Ccode>const SystemRolePrompt = `You are a Go code explanation assistant. \nPlease provide concise answers and focus on code examples that follow Go Best Practices.`\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>B. Using text\u002Ftemplate (For Dynamic Prompts)\u003C\u002Fh3>\u003Cp>In cases where you need \u003Cstrong>Data Injection\u003C\u002Fstrong>—such as inserting a username or article content into the prompt—using \u003Ccode>fmt.Sprintf\u003C\u002Fcode> can get messy as the prompt grows. The \u003Ccode>text\u002Ftemplate\u003C\u002Fcode> package keeps your code clean and provides a much better structure for managing complex prompts.\u003C\u002Fp>\u003Cp>Go\u003C\u002Fp>\u003Cpre>\u003Ccode>const SummaryTemplate = `Summarize the article titled \"{{.Title}}\" \nFocus on 3 key points: {{.Content}}`\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>💡 Pro-Tips from Experience:\u003C\u002Fh3>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>Separate Your Files:\u003C\u002Fstrong> As your project grows, I recommend creating a dedicated \u003Ccode>prompts.go\u003C\u002Fcode> file. This makes it easier for the team (or even a dedicated Prompt Engineer) to refine the messaging without digging through the core logic of the program.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Version Control:\u003C\u002Fstrong> Separating prompts into constants makes changes visible through Git Diff, allowing you to track exactly how a prompt was \"tuned\" over time.\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch2>Output Formatting: Enforcing JSON Responses\u003C\u002Fh2>\u003Cp>The most frustrating issue for developers is when an AI is \"too polite\"—prefixing its response with introductions like, \u003Cem>\"Certainly! Here is the JSON you requested...\"\u003C\u002Fem> These extra sentences cause Go's \u003Ccode>json.Unmarshal\u003C\u002Fcode> function to throw an error immediately.\u003C\u002Fp>\u003Ch3>Techniques for Precise Output Control:\u003C\u002Fh3>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>Enforce with Negative Prompting:\u003C\u002Fstrong> Besides asking for JSON, explicitly state: \u003Cstrong>\"No prose, no explanations, just raw JSON\"\u003C\u002Fstrong> to cut out the chatter.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Provide a JSON Schema:\u003C\u002Fstrong> AI performs significantly better when it can visualize the exact structure you require.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Enable JSON Mode:\u003C\u002Fstrong> If you are using OpenAI or the latest Ollama versions, set the API Request to \u003Ccode>response_format: { \"type\": \"json_object\" }\u003C\u002Fcode>. This forces the engine to return only valid, parsable JSON.\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch3>Example Structure in Go:\u003C\u002Fh3>\u003Cp>Go\u003C\u002Fp>\u003Cpre>\u003Ccode>const JSONPrompt = `Summarize this article in JSON format only.\nRules: Do not include any introduction or explanation. Output raw JSON only.\nSchema: {\n  \"title\": \"string\",\n  \"summary\": \"string\", \n  \"tags\": [\"string\", \"string\"]\n}`\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>⚠️ Pro Tip for Gophers: Handling the Markdown Trap\u003C\u002Fh3>\u003Cp>Even when told \"JSON only,\" some AIs still wrap their response in Markdown code blocks (e.g., \u003Ccode>json ... \u003C\u002Fcode>).\u003C\u002Fp>\u003Cp>\u003Cstrong>The Solution:\u003C\u002Fstrong> Your Go code should include a small helper function to trim or strip away these backticks and the \"json\" identifier from the string before attempting to unmarshal. This makes your system robust against minor AI inconsistencies.\u003C\u002Fp>\u003Ch2>Few-Shot Prompting: Teaching with Examples\u003C\u002Fh2>\u003Cp>Sometimes, instructions alone aren't enough. Few-Shot Prompting involves sending a set of example inputs and outputs to the AI, giving it a clear pattern to follow before it answers the actual query.\u003C\u002Fp>\u003Ch3>Why use Few-Shot?\u003C\u002Fh3>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>Control Response Style:\u003C\u002Fstrong> For example, keeping answers strictly professional or intentionally casual.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Master Complex Formats:\u003C\u002Fstrong> Helps the AI understand intricate data structures that are hard to describe in words.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Reduce Errors:\u003C\u002Fstrong> When the AI sees a correct example, it naturally mirrors that pattern.\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch3>Example Implementation in Go:\u003C\u002Fh3>\u003Cp>We implement this by passing a slice of messages that simulate a previous chat history.\u003C\u002Fp>\u003Cp>Go\u003C\u002Fp>\u003Cpre>\u003Ccode>messages := []openai.ChatCompletionMessage{\n    \u002F\u002F Example 1\n    {Role: \"user\", Content: \"What is Go?\"},\n    {Role: \"assistant\", Content: \"Go is a programming language focused on simplicity and efficiency.\"},\n    \n    \u002F\u002F Example 2\n    {Role: \"user\", Content: \"And what is Docker?\"},\n    {Role: \"assistant\", Content: \"Docker is a platform for managing containers to run apps anywhere.\"},\n    \n    \u002F\u002F The Real Question\n    {Role: \"user\", Content: \"What is Kubernetes?\"}, \n    \u002F\u002F The AI will now respond in that same \"brief definition\" style!\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>💡 Tips for Gophers:\u003C\u002Fh3>\u003Cp>In a production environment, you can store these example sets in JSON files or a database. You can then dynamically load and inject them into your \u003Ccode>[]openai.ChatCompletionMessage\u003C\u002Fcode> before hitting the API. This allows you to \"re-tune\" your AI's behavior without needing to re-compile your code!\u003C\u002Fp>\u003Ch2>Chain of Thought: Forcing the AI to Think Before Answering\u003C\u002Fh2>\u003Cp>Have you ever asked an AI to write a complex algorithm, only to receive code that doesn't run or contains baffling logical errors? This happens because AI typically operates on Next Token Prediction—it tries to generate the answer immediately without \"planning\" the logic first.\u003C\u002Fp>\u003Cp>Chain of Thought (CoT) is a technique that instructs the AI to break down its logic before summarizing the final answer. This significantly boosts accuracy.\u003C\u002Fp>\u003Ch3>Prompting Techniques:\u003C\u002Fh3>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>The Golden Phrase:\u003C\u002Fstrong> Simply add \u003Cstrong>\"Let’s think step by step\"\u003C\u002Fstrong> or \u003Cstrong>\"Please reason through this logically step by step.\"\u003C\u002Fstrong>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Structured Thinking:\u003C\u002Fstrong> If you need a JSON output but still want the AI to \"think\" first, instruct it to divide its response into two parts: \u003Ccode>thoughts\u003C\u002Fcode> (for the reasoning) and \u003Ccode>result\u003C\u002Fcode> (for the final data).\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch3>Example in Go:\u003C\u002Fh3>\u003Cp>Go\u003C\u002Fp>\u003Cpre>\u003Ccode>const ComplexLogicPrompt = `Write a Go function to calculate progressive income tax.\nPlease \"think step by step\" as follows:\n1. Analyze each tax bracket condition.\n2. Define the necessary variable structures.\n3. Write the calculation logic.\n4. Check for edge cases (e.g., zero income, max bracket).\n5. Summarize into a functional Go code block.`\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>💡 Why should Gophers use CoT?\u003C\u002Fh3>\u003Cp>In backend development, we often use AI for code reviews or debugging. By forcing the AI to \"explain its reasoning\" before providing the fix, we can verify if the AI actually understands our business logic or if it's just making a lucky guess that might fail in production.\u003C\u002Fp>\u003Ch2>🎯 Daily Mission\u003C\u002Fh2>\u003Cp>To see the power of Prompt Engineering for yourself, try creating a Go function that takes raw article text as input and uses a prompt to force the AI to summarize it into a \u003Cstrong>JSON Struct\u003C\u002Fstrong> containing:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Ccode>title\u003C\u002Fcode> (The headline)\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Ccode>abstract\u003C\u002Fcode> (A short summary)\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Ccode>category\u003C\u002Fcode> (The topic category)\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch3>Homework Challenge:\u003C\u002Fh3>\u003Cp>Refine your prompt until the AI returns a JSON string clean enough to be used with \u003Ccode>json.Unmarshal\u003C\u002Fcode> into your Go struct \u003Cstrong>100% of the time\u003C\u002Fstrong> without a single error!\u003C\u002Fp>\u003Cdiv data-type=\"horizontalRule\">\u003Chr>\u003C\u002Fdiv>\u003Ch2>Conclusion: From Commanding to Controlling\u003C\u002Fh2>\u003Cp>Prompt Engineering for Gophers isn't about \"prompt art\" or using fancy words. It is about \u003Cstrong>Designing a Data Interface\u003C\u002Fstrong> so the AI can communicate with our program effectively.\u003C\u002Fp>\u003Cp>Remember, a great prompt within your source code must be:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>Readable\u003C\u002Fstrong> (Structured)\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Precise\u003C\u002Fstrong> (JSON)\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Patterned\u003C\u002Fstrong> (Few-Shot)\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Logical\u003C\u002Fstrong> (CoT)\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>When you master these factors, AI ceases to be just a \"chatbot\" and becomes an intelligent, reliable module within your backend ecosystem.\u003C\u002Fp>\u003Ch3>Coming Up Next | EP.147: Structured Output — Forcing JSON for 100% Parsing Accuracy\u003C\u002Fh3>\u003Cp>In this episode, we learned the \u003Cem>art\u003C\u002Fem> of writing prompts. However, in the real world, AI can still go rogue or return weirdly formatted JSON that can cause your Go code to panic!\u003C\u002Fp>\u003Cp>If you want to build a system that never breaks regardless of how the AI feels, don't miss \u003Cstrong>EP.147\u003C\u002Fstrong>! We’ll dive into deep-level JSON enforcement and error handling.\u003C\u002Fp>\u003Cp>\u003Cstrong>Follow Superdev Academy on all platforms:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>Superdev Academy Thailand\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🎬 YouTube: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.youtube.com\u002F@SuperdevAcademy\">\u003Cstrong>Superdev Academy Channel\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevacademy\u002F\">\u003Cstrong>@superdevacademy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>@superdevacademy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"http:\u002F\u002Fsuperdevacademy.com\">\u003Cstrong>superdevacademy.com\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003C\u002Fp>","126j7feovc1i_qg9ydl689f.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fnykxzlg5dfgphz4\u002F126j7feovc1i_qg9ydl689f.png","2026-05-19 08:15:43.176Z","",{"keywords":15,"locale":41,"school_blog":51},[16,23,28,32,37],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:44:51.471Z","hlato0hav8vo8wm","Golang The Series","2026-04-10 16:12:50.850Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":29,"updated_by":13},"2026-05-19 08:10:08.033Z","b5bcgkramrz2ogu","Prompt Engineering",{"collectionId":17,"collectionName":18,"created":33,"created_by":13,"id":34,"name":35,"updated":36,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":38,"created_by":13,"id":39,"name":40,"updated":38,"updated_by":13},"2026-05-19 08:10:19.398Z","6gd8t3k2sfam302","JSON Output",{"code":42,"collectionId":43,"collectionName":44,"created":45,"flag":46,"id":47,"is_default":48,"label":49,"updated":50},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":52,"collectionId":53,"collectionName":54,"created":55,"expand":56,"id":69,"slug":70,"updated":71,"views":72},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs","2026-05-19 08:10:32.555Z",{"category":57},{"blogIds":58,"collectionId":59,"collectionName":60,"created":61,"created_by":13,"id":52,"image":62,"image_alt":13,"image_path":63,"label":64,"name":21,"priority":65,"publish_at":66,"scheduled_at":13,"status":67,"updated":68,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":21,"th":21},1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","k7elykum3z2xaw7","golang-the-series-ep146-prompt-engineering-for-gophers","2026-05-25 10:35:19.633Z",114,"nykxzlg5dfgphz4",[20,25,30,34,39],"2026-05-25 09:00:00.000Z","Transform how you integrate AI by mastering Prompt Engineering in Go. Learn how to enforce JSON outputs, use Few-Shot examples, and apply Chain-of-Thought within your backend applications.","Golang The Series EP.146: Prompt Engineering for Gophers - Mastering AI in Code","2026-05-25 09:00:00.139Z",{"th":70,"en":70}]