View : 0
04/03/2026 08:27am

JS2GO EP.2 The Basics: How JavaScript and Go Differ?
#JavaScript
#Go
#JavaScript vs Go
#Error Handling
#Syntax
#programming languages
#Superdev School
In EP.1, we started by getting to know Go and explored some of the basic features that make Go interesting and different from JavaScript. Now, let’s take a closer look at how JavaScript and Go differ in terms of syntax, error handling, and design principles. We’ll use code examples to clearly illustrate which language is more suited for certain tasks.
Comparison of Syntax Between JavaScript and Go
Declaring Variables:
JavaScript:
let name = "John";
const age = 30;
Go:
// Inside a function
func main() {
var name = "John" // Declaring a variable outside the function
age := 30 // Using := inside the function
}
Explanation:
In JavaScript, you can use let or const to declare variables:
letallows you to reassign the variable’s value.constis used for variables that should remain constant and cannot be reassigned.
In Go, variables can be declared using var or :=:
varis used to explicitly declare a variable, with or without specifying its type.:=is shorthand for declaring a variable and assigning it a value, with Go inferring the type automatically.
Using Functions:
JavaScript:
function greet(name) {
return "Hello " + name;
}
Go:
func greet(name string) string { // Specifying the types of input and output variables
return "Hello " + name
}
Explanation:
In JavaScript, functions can be declared easily without specifying the types of the parameters or return values.
In Go, functions require the types of parameters and return values to be explicitly specified, making the code clearer and easier to check for type errors.
Control Flow (if-else):
JavaScript (if-else):
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
Go (if-else):
if age >= 18 {
fmt.Println("Adult")
} else {
fmt.Println("Minor")
}
Explanation:
Both languages use the if-else structure for control flow, but in Go, we always use {} to enclose the block of statements. In JavaScript, curly braces can be optional for single-line blocks, depending on the number of statements.
การจัดการข้อผิดพลาด (Error Handling)
JavaScript:
try {
let result = someFunction();
} catch (error) {
console.error("Error:", error.message);
}
Go:
result, err := someFunction()
if err != nil {
fmt.Println("Error:", err)
return
}
คำอธิบาย:
- JavaScript ใช้
try-catchblock เพื่อจับข้อผิดพลาดที่อาจเกิดขึ้นในช่วงเวลาที่โค้ดทำงาน โดยtryจะลองทำงานโค้ดภายใน และถ้าพบข้อผิดพลาดจะเข้าสู่catchซึ่งสามารถจัดการหรือแสดงข้อผิดพลาดได้ - Go ใช้การตรวจสอบค่าผลลัพธ์จากฟังก์ชันที่คืนค่าผลลัพธ์พร้อมกับ
errorโดยตรง ซึ่งหมายความว่า Go จะตรวจสอบข้อผิดพลาดทันทีที่ฟังก์ชันทำงานเสร็จ ผลลัพธ์ที่ได้จากฟังก์ชันจะถูกตรวจสอบและหากมีข้อผิดพลาดจะทำการจัดการทันที ช่วยให้การจัดการข้อผิดพลาดใน Go ชัดเจนและระบุปัญหาได้ง่าย
หลักการออกแบบและการใช้งาน
JavaScript:
- Event-driven programming: JavaScript เหมาะสำหรับการเขียนโปรแกรมที่ตอบสนองต่อเหตุการณ์ (events) เช่น การคลิกปุ่ม, การพิมพ์ข้อความ หรือการเปลี่ยนแปลงในหน้าเว็บ
- Asynchronous programming: JavaScript รองรับการเขียนโปรแกรมแบบอะซิงโครนัส (เช่น การใช้ Promises หรือ async/await) ซึ่งช่วยให้โปรแกรมทำงานได้ต่อเนื่องโดยไม่ต้องหยุดรอผลลัพธ์จากฟังก์ชันต่าง ๆ
Go:
- Concurrency: Go ได้รับการออกแบบให้รองรับการทำงานแบบขนาน (concurrent) โดยใช้ goroutines และ channels ซึ่งหมายความว่า คุณสามารถรันกระบวนการหลายตัวพร้อมกันได้อย่างมีประสิทธิภาพ
- Efficiency: Go เน้นการเขียนโปรแกรมที่ทำงานได้เร็วและมีประสิทธิภาพสูง เหมาะกับงานที่ต้องการการประมวลผลจำนวนมาก หรือระบบที่มีการเชื่อมต่อแบบเครือข่าย (เช่น microservices และ APIs)
Use Cases และเมื่อไหร่ควรใช้แต่ละภาษา
JavaScript:
- JavaScript เหมาะสำหรับการพัฒนา เว็บแอปพลิเคชัน ทั้งฝั่ง front-end และ back-end โดยเฉพาะในงานที่มีการโต้ตอบกับผู้ใช้ หรือมีการอัพเดทข้อมูลแบบเรียลไทม์ เช่น การทำ real-time apps หรือ single-page applications (SPA)
Go:
- Go เหมาะสำหรับการพัฒนา backend ที่ต้องการการประมวลผลที่รวดเร็วและรองรับ microservices, APIs, และ concurrent tasks ที่มีประสิทธิภาพสูง เช่น การพัฒนา cloud services หรือระบบที่ต้องการการจัดการข้อมูลจำนวนมาก
Summary
By now, you should have a clear understanding of the differences between JavaScript and Go in terms of Syntax, Error Handling, and Design Principles. If you're eager to dive deeper into Go, or if you're looking to start developing APIs, Microservices, or explore Concurrency, don’t wait any longer! Sign up at Superdev School today to enhance your programming skills!
Next Up:
In the next episode of the JS2GO series, we’ll guide you through Transitioning from JavaScript to Go: What You Need to Know Before Getting Started with Go. In this article, we’ll cover the steps and important considerations before you begin working with Go, including the tools you should prepare, system setup, and an introduction to Go Modules, Go runtime, and how to use Golang effectively.
Get ready for the journey from JavaScript to Go, and learn how to correctly set up a Go project, so you can start coding smoothly and efficiently!
Read more Golang articles: Golang The Series
Read more JS2GO articles: JS2GO
🔵 Facebook: Superdev School (Superdev)
📸 Instagram: superdevschool
🎬 TikTok: superdevschool
🌐 Website: www.superdev.school