From b074f149fe305b857b04ccf5a072bd8bed30ecbd Mon Sep 17 00:00:00 2001 From: ErdemOzgen <12345678+username@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:35:35 +0300 Subject: [PATCH] update golang --- ...ang - Asterisk and Ampersand Cheatsheet.md | 383 ++++++++++++++++++ .../golang}/Golang Cobra and Viper.md | 0 .../golang}/Golang context.md | 0 3 files changed, 383 insertions(+) create mode 100644 content/ProgramingLang/golang/Golang - Asterisk and Ampersand Cheatsheet.md rename content/{SoftwareEngineering => ProgramingLang/golang}/Golang Cobra and Viper.md (100%) rename content/{SoftwareEngineering => ProgramingLang/golang}/Golang context.md (100%) diff --git a/content/ProgramingLang/golang/Golang - Asterisk and Ampersand Cheatsheet.md b/content/ProgramingLang/golang/Golang - Asterisk and Ampersand Cheatsheet.md new file mode 100644 index 000000000..94ff82ed9 --- /dev/null +++ b/content/ProgramingLang/golang/Golang - Asterisk and Ampersand Cheatsheet.md @@ -0,0 +1,383 @@ + + +******************************************************************************** +Golang - Asterisk and Ampersand Cheatsheet +******************************************************************************** + +Also available at: https://play.golang.org/p/lNpnS9j1ma + +Allowed: +-------- +```go +/* + +******************************************************************************** + +Golang - Asterisk and Ampersand Cheatsheet + +******************************************************************************** + + + +Allowed: + +-------- + +p := Person{"Hillary", 28}  stores the value + +p := &Person{"Hillary", 28}     stores the pointer address (reference) + +PrintPerson(p)          passes either the value or pointer address (reference) + +PrintPerson(*p)         passes the value + +PrintPerson(&p)         passes the pointer address (reference) + +func PrintPerson(p Person)  ONLY receives the value + +func PrintPerson(p *Person) ONLY receives the pointer address (reference) + + + +Not Allowed: + +-------- + +p := *Person{"Hillary", 28}     illegal + +func PrintPerson(p &Person) illegal + + + +*/ + + + +package main + + + +import ( + +    "fmt" + +) + + + +type Erdem struct { + +    CodingSkillPercent int + +    Occupation         string + +} + +type Person struct { + +    Name string + +    Age  int + +} + + + +// This only works with *Person, does not work with Person + +// Only works with Test 2 and Test 3 + +func (p *Person) String() string { + +    return fmt.Sprintf("%s is %d", p.Name, p.Age) + +} + + + +// This works with both *Person and Person, BUT you can't modiy the value and + +// it takes up more space + +// Works with Test 1, Test 2, Test 3, and Test 4 + +/*func (p Person) String() string { + +    return fmt.Sprintf("%s is %d", p.Name, p.Age) + +}*/ + + + +// ***************************************************************************** + +// Test 1 - Pass by Value + +// ***************************************************************************** + + + +func test1() { + +    fmt.Println("Test 1") + +    p := Person{"Hillary", 28} + +    printPerson1(p) + +    updatePerson1(p) + +    printPerson1(p) + +    fmt.Println("Test 1 ENDS") + +} + + + +func updatePerson1(p Person) { + +    p.Age = 32 + +    printPerson1(p) + +} + + + +func printPerson1(p Person) { + +    fmt.Printf("String: %v | Name: %v | Age: %d\n", + +        p, + +        p.Name, + +        p.Age) + +} + + + +// ***************************************************************************** + +// Test 2 - Pass by Reference + +// ***************************************************************************** + + + +func test2() { + +    fmt.Println("Test 2") + +    p := &Person{"Hillary", 28} + +    printPerson2(p) + +    updatePerson2(p) + +    printPerson2(p) + +    fmt.Println("Test 2 ENDS") + +} + + + +func updatePerson2(p *Person) { + +    p.Age = 32 + +    printPerson2(p) + +} + + + +func printPerson2(p *Person) { + +    fmt.Printf("String: %v | Name: %v | Age: %d\n", + +        p, + +        p.Name, + +        p.Age) + +} + + + +// ***************************************************************************** + +// Test 3 - Pass by Reference (requires more typing) + +// ***************************************************************************** + + + +func test3() { + +    fmt.Println("Test 3") + +    p := Person{"Hillary", 28} + +    printPerson3(&p) + +    updatePerson3(&p) + +    printPerson3(&p) + +    fmt.Println("Test 3 ENDS") + +} + + + +func updatePerson3(p *Person) { + +    p.Age = 32 + +    printPerson3(p) + +} + + + +func printPerson3(p *Person) { + +    fmt.Printf("String: %v | Name: %v | Age: %d\n", + +        p, + +        p.Name, + +        p.Age) + +} + + + +// ***************************************************************************** + +// Test 4 - Pass by Value (requires more typing) + +// ***************************************************************************** + + + +func test4() { + +    p := &Person{"Hillary", 28} + +    printPerson4(*p) + +    updatePerson4(*p) + +    printPerson4(*p) + +} + + + +func updatePerson4(p Person) { + +    p.Age = 32 + +    printPerson4(p) + +} + + + +func printPerson4(p Person) { + +    fmt.Printf("String: %v | Name: %v | Age: %d\n", + +        p, + +        p.Name, + +        p.Age) + +} + + + +// ***************************************************************************** + +// Main + +// ***************************************************************************** + + + +/* + +Outputs: + +String: {Hillary 28} | Name: Hillary | Age: 28 + +String: {Hillary 32} | Name: Hillary | Age: 32 + +String: {Hillary 28} | Name: Hillary | Age: 28 + +String: Hillary is 28 | Name: Hillary | Age: 28 + +String: Hillary is 32 | Name: Hillary | Age: 32 + +String: Hillary is 32 | Name: Hillary | Age: 32 + +String: Hillary is 28 | Name: Hillary | Age: 28 + +String: Hillary is 32 | Name: Hillary | Age: 32 + +String: Hillary is 32 | Name: Hillary | Age: 32 + +String: {Hillary 28} | Name: Hillary | Age: 28 + +String: {Hillary 32} | Name: Hillary | Age: 32 + +String: {Hillary 28} | Name: Hillary | Age: 28 + +*/ + +func main() { + +    test1() + +    test2() + +    test3() + +    test4() + +} + +// ***************************************************************************** +// Main +// ***************************************************************************** + +/* +Outputs: +String: {Steve 28} | Name: Steve | Age: 28 +String: {Steve 32} | Name: Steve | Age: 32 +String: {Steve 28} | Name: Steve | Age: 28 +String: Steve is 28 | Name: Steve | Age: 28 +String: Steve is 32 | Name: Steve | Age: 32 +String: Steve is 32 | Name: Steve | Age: 32 +String: Steve is 28 | Name: Steve | Age: 28 +String: Steve is 32 | Name: Steve | Age: 32 +String: Steve is 32 | Name: Steve | Age: 32 +String: {Steve 28} | Name: Steve | Age: 28 +String: {Steve 32} | Name: Steve | Age: 32 +String: {Steve 28} | Name: Steve | Age: 28 +*/ +func main() { + test1() + test2() + test3() + test4() +} +``` diff --git a/content/SoftwareEngineering/Golang Cobra and Viper.md b/content/ProgramingLang/golang/Golang Cobra and Viper.md similarity index 100% rename from content/SoftwareEngineering/Golang Cobra and Viper.md rename to content/ProgramingLang/golang/Golang Cobra and Viper.md diff --git a/content/SoftwareEngineering/Golang context.md b/content/ProgramingLang/golang/Golang context.md similarity index 100% rename from content/SoftwareEngineering/Golang context.md rename to content/ProgramingLang/golang/Golang context.md