Comparing Data Types in Go and PHP
Basic Data Types
Go
- Boolean
- Numeric types:
- Integer (int, uint)
- Floating-point (float32, float64)
- Others: byte (similar to uint8), rune (similar to int32)
- String
- Derived types:
- Pointer
- Array
- Struct
- Channel
- Function
- Slice
- Enterface
- Map
PHP
- 4 scalar types:
- boolean
- integer
- float (also called double)
- string
- 3 compound types:
- array
- object
- callable
- 2 special types:
- resource
- NULL
String Handling
Byte Encoding
Go
- Strings use UTF-8 encoding by default
- Supports Unicode characters
PHP
- Supports only 256-character set
- Requires explicit character encoding specification:
header("Content-Type: text/html;charset=utf-8");or modifyingdefault_charset = "utf-8"in php.ini - No Unicode character support
String Definition
Go
- Double quotes (
""): Escape sequences are processed - Backticks (``): Raw strings where escape sequences are preserved
- Single quotes (
''): Single characters requiring formatting with%c
PHP
- Double quotes (
""): Escape sequences processed and variables parsed - Single quotes (
''): Similar to Go's backticks, escape sequences preserved
String Length
Go
len(): Returns UTF-8 encoded byte count- Example:
len("hello,世界")returns 12, not 8 utf8.RuneCountInString(): Returns actual character count- Requires import:
"unicode/utf8"
PHP
strlen(): Similar to Go'slen(), returns byte count- Actual character count functions:
mb_strlen(): Requires mbstring extensioniconv_strlen(): Returns character count as integer
String Concatenation
Go
-
Operator +:
text := "hello, " + "world"- Plus operator for concatenation
- Line breaks require + at line end
- Creates temporary strings, poor performance
-
fmt.Sprintf():
result := fmt.Sprintf("%d:%s", 2024, "year")- Uses []byte internally, no temporary strings
- Uses interfaces, moderate performance
-
strings.Join():
combined := strings.Join([]string{"hello", "world"}, ", ")- Efficient with existing arrays
- Calculates length before allocation
-
bytes.Buffer (Recommended):
var buffer bytes.Buffer buffer.WriteString("hello") buffer.WriteString(", ") buffer.WriteString("world") fmt.Print(buffer.String())- Mutable string handling
- Memory growth optimization
- Use buffer.Grow() for capacity predefinition
-
strings.Builder (Recommended):
var builder strings.Builder builder.WriteString("ABC") builder.WriteString("DEF") fmt.Print(builder.String())- Uses slice for content management
- Grow() for capacity predefinition
- Not thread-safe, similar performance to bytes.Buffer
PHP
- Operator .:
$text = "hello, " . "world"; // Or: $text = "hello, " . "world";- Dot operator for concatenation
- No line break restrictions
Arrays
Array Declaration
Go
- Elements must be same type
- Use interface{} for mixed types with type assertions
- Length determination required
Declaration methods:
var numbers [2]int
var numbers = [2]int{1, 2}
var words = [5]string{3: "ab", 4: "cd"}
var numbers = [...]int{1, 2}
var arrPtr = new([5]int) // Array pointer
PHP
- No length specification required
- Mixed element types allowed
$items = ['a', 'b', 'c', 123];
Value vs Reference Pasing
Go
var arrPtr = new([5]int)
arr := arrPtr
arrPtr[2] = 100
fmt.Println(arrPtr[2], arr[2]) // 100 100
var arr2 [5]int
newArr := arr2
arr2[2] = 100
fmt.Println(arr2[2], newArr[2]) // 100 0
new()creates array pointers (reference)- Direct assignment creates copies (value)
PHP
$arr1 = [5, 10, 0, 20, 25];
$arr = &$arr1; // Reference
$newArr = $arr1; // Value copy
$arr1[2] = 100;
echo $arr1[2], $arr[2]; // 100 100
echo $arr1[2], $newArr[2]; // 100 0
&operator for reference passing
Slices
Go
- References to underlying array segments
- No extra memory overhead, more efficient than arrays
- Uninitialized slices are nil with length 0
- Variable langth, like dynamic arrays
Declaration methods:
var numbers = [5]int{1, 2, 3, 4, 5}
var slice []int = numbers[start:end:max]
var primes = []int{2, 3, 5, 7, 11}
var data = make([]int, length, capacity)
var simple = make([]int, length)
PHP
- No native slice concept
array_slice()function provides similar functionality:
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 0, 3); // "a", "b", "c"
Maps/Dictionaries
Go
- Unordered collection of key-value pairs
- Uninitialized maps are nil
- Reference type
- Dynamic growth, no length specification
Declaration methods:
var dataMap map[keyType]valueType
var dataMap = map[keyType]valueType{k1:v1, k2:v2}
var dataMap = make(map[keyType]valueType, capacity)
PHP
- Associative arrays function similarly to Go maps