Skip to content

Instantly share code, notes, and snippets.

func parseWithStringManipulation(inputString string) int {
isAm := strings.HasSuffix(inputString, "AM")
number, _ := strconv.Atoi(inputString[:len(inputString) - 2])
if !isAm {
number += 12
}
return number
}
func BenchmarkParseWithStringManipulation(b *testing.B) {
for n := 0; n < b.N; n++ {
parseWithStringManipulation("7PM")
}
}
func parseWithStringManipulationWithoutHasSuffix(inputString string) int {
isAm := inputString[len(inputString)-2] == 'A'
number, _ := strconv.Atoi(inputString[:len(inputString) - 2])
if !isAm {
number += 12
}
return number
}
func parseWithBlackMagic(inputString string) int {
isAm := inputString[len(inputString)-2] == 'A'
number := int(inputString[0]) - '0'
if len(inputString) > 3 {
number = number*10 + int(inputString[1]) - '0'
}
if !isAm {
number += 12
class Person {
public string $firstName;
public string $lastName;
public int $age;
public ?string $job;
}
class Person {
private string $firstName;
private string $lastName;
protected int $age;
public ?string $job;
public function __construct(
class Person {
public function __construct(
private string $firstName,
private string $lastName,
protected int $age,
public ?string $job
){}
}
<?
if (is_null($repository)) {
$result = null;
} else {
$user = $repository->getUser(5);
if (is_null($user)) {
$result = null;
} else {
$result = $user->name;
}
$result = $repository?->getUser(5)?->name;
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"