Dark mode
ประเภทข้อมูล (Data Types)
ประเภทข้อมูลเป็นพื้นฐานสำคัญในการเขียนโปรแกรม ทุกภาษาโปรแกรมมีประเภทข้อมูลพื้นฐานที่คล้ายกัน แม้ว่ารายละเอียดการใช้งานอาจแตกต่างกันไปตามแต่ละภาษา
Numbers (ตัวเลข)
js
// ตัวเลขใน JavaScript
const integer = 42; // จำนวนเต็ม
const float = 3.14; // ทศนิยม
const bigInt = 9007199254740991n; // จำนวนเต็มขนาดใหญ่
console.log(typeof integer); // "number"
console.log(typeof float); // "number"
console.log(typeof bigInt); // "bigint"
rust
// ตัวเลขใน Rust
fn main() {
let integer: i32 = 42; // จำนวนเต็ม 32-bit
let float: f64 = 3.14; // ทศนิยม 64-bit
println!("{}", integer);
println!("{}", float);
}
python
# ตัวเลขใน Python
integer = 42 # จำนวนเต็ม
float_num = 3.14 # ทศนิยม
big_int = 10**100 # จำนวนเต็มขนาดใหญ่
print(type(integer)) # <class 'int'>
print(type(float_num)) # <class 'float'>
print(type(big_int)) # <class 'int'>
go
// ตัวเลขใน Go
package main
import "fmt"
func main() {
var integer int = 42 // จำนวนเต็ม
var float float64 = 3.14 // ทศนิยม
fmt.Println(integer)
fmt.Println(float)
}
Strings (ข้อความ)
js
// String in JavaScript
const name = "John"; // ข้อความแบบใช้เครื่องหมาย single quote
const greeting = `Hello ${name}`; // ข้อความแบบ Template literal ที่สามารถแทรกตัวแปรได้
console.log(greeting); // "Hello John"
console.log(greeting.length); // 10
rust
// String in Rust
fn main() {
let name = "John"; // &str (string slice - ข้อความแบบอ้างอิง)
let mut greeting = String::from("Hello "); // String (ข้อความแบบปรับเปลี่ยนได้)
greeting.push_str(name); // เพิ่มข้อความต่อท้าย
println!("{}", greeting); // "Hello John"
println!("{}", greeting.len()); // 10
}
python
# String in Python
name = 'John' # ข้อความแบบ single quote
greeting = f'Hello {name}' # f-string ที่สามารถแทรกตัวแปรได้
print(greeting) # "Hello John"
print(len(greeting)) # 10
go
// String in Go
package main
import "fmt"
func main() {
name := "John" // การประกาศตัวแปรแบบสั้น
greeting := "Hello " + name // ต่อข้อความด้วยเครื่องหมาย +
fmt.Println(greeting) // "Hello John"
fmt.Println(len(greeting)) // 10
}
Booleans (บูลีน)
js
// Boolean in JavaScript
const isTrue = true; // ค่าความจริง
const isFalse = false; // ค่าความเท็จ
console.log(typeof isTrue); // "boolean"
console.log(isTrue && isFalse); // false (logical AND operation)
rust
// Boolean in Rust
fn main() {
let is_true: bool = true; // ค่าความจริง
let is_false: bool = false; // ค่าความเท็จ
println!("{}", is_true); // true
println!("{}", is_true && is_false); // false (logical AND operation)
}
python
# Boolean in Python
is_true = True # ค่าความจริง
is_false = False # ค่าความเท็จ
print(type(is_true)) # <class 'bool'>
print(is_true and is_false) # False (logical AND operation)
go
// Boolean in Go
package main
import "fmt"
func main() {
isTrue := true // ค่าความจริง
isFalse := false // ค่าความเท็จ
fmt.Println(isTrue) // true
fmt.Println(isTrue && isFalse) // false (logical AND operation)
}
Arrays & Collections (อาร์เรย์และคอลเลกชัน)
js
// Array in JavaScript
const fruits = ["apple", "banana", "orange"]; // การสร้างอาร์เรย์
console.log(fruits[0]); // "apple" (accessing first element)
console.log(fruits.length); // 3 (total number of elements)
// Adding a new element
fruits.push("mango"); // เพิ่มสมาชิกใหม่ต่อท้ายอาร์เรย์
console.log(fruits); // ['apple', 'banana', 'orange', 'mango']
rust
// Vector in Rust
fn main() {
let mut fruits = vec!["apple", "banana", "orange"]; // การสร้างเวกเตอร์
println!("{}", fruits[0]); // "apple" (accessing first element)
println!("{}", fruits.len()); // 3 (total number of elements)
// Adding a new element
fruits.push("mango"); // เพิ่มสมาชิกใหม่ต่อท้ายเวกเตอร์
println!("{:?}", fruits); // ["apple", "banana", "orange", "mango"]
}
python
# List in Python
fruits = ["apple", "banana", "orange"] # การสร้างลิสต์
print(fruits[0]) # "apple" (accessing first element)
print(len(fruits)) # 3 (total number of elements)
# Adding a new element
fruits.append("mango") # เพิ่มสมาชิกใหม่ต่อท้ายลิสต์
print(fruits) # ['apple', 'banana', 'orange', 'mango']
go
// Slice in Go
package main
import "fmt"
func main() {
fruits := []string{"apple", "banana", "orange"} // การสร้างสไลซ์
fmt.Println(fruits[0]) // "apple" (accessing first element)
fmt.Println(len(fruits)) // 3 (total number of elements)
// Adding a new element
fruits = append(fruits, "mango") // เพิ่มสมาชิกใหม่ต่อท้ายสไลซ์
fmt.Println(fruits) // [apple banana orange mango]
}
Objects & Structs (ออบเจ็กต์และโครงสร้าง)
js
// Object in JavaScript
const person = {
name: "John", // การกำหนดคู่ property-value
age: 30,
address: { // nested object
city: "Bangkok",
},
};
console.log(person.name); // "John" (accessing with dot notation)
console.log(person.address.city); // "Bangkok"
rust
// Struct in Rust
struct Address {
city: String, // การกำหนดฟิลด์
}
struct Person {
name: String,
age: u32,
address: Address, // nested struct
}
fn main() {
let person = Person { // การสร้าง instance ของ struct
name: String::from("John"),
age: 30,
address: Address {
city: String::from("Bangkok"),
},
};
println!("{}", person.name); // "John" (accessing with dot notation)
println!("{}", person.address.city); // "Bangkok"
}
python
# Dictionary and Class in Python
# Using dictionary (similar to JS object)
person_dict = {
'name': 'John',
'age': 30,
'address': {
'city': 'Bangkok'
}
}
print(person_dict['name']) # "John"
print(person_dict['address']['city']) # "Bangkok"
# Using class (similar to struct in other languages)
class Address:
def __init__(self, city):
self.city = city
class Person:
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
# Create instances
address = Address("Bangkok")
person = Person("John", 30, address)
print(person.name) # "John"
print(person.address.city) # "Bangkok"
go
// Struct in Go
package main
import "fmt"
type Address struct {
City string // การกำหนดฟิลด์
}
type Person struct {
Name string
Age int
Address Address // nested struct
}
func main() {
person := Person{ // การสร้าง instance ของ struct
Name: "John",
Age: 30,
Address: Address{
City: "Bangkok",
},
}
fmt.Println(person.Name) // "John" (accessing with dot notation)
fmt.Println(person.Address.City) // "Bangkok"
}