golang Golang - Asterisk和Ampersand Cheatsheet

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang Golang - Asterisk和Ampersand Cheatsheet相关的知识,希望对你有一定的参考价值。

/*
********************************************************************************
Golang - Asterisk and Ampersand Cheatsheet
********************************************************************************

Also available at: https://play.golang.org/p/lNpnS9j1ma

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 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() {
	p := Person{"Hillary", 28}
	printPerson1(p)
	updatePerson1(p)
	printPerson1(p)
}

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() {
	p := &Person{"Hillary", 28}
	printPerson2(p)
	updatePerson2(p)
	printPerson2(p)
}

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() {
	p := Person{"Hillary", 28}
	printPerson3(&p)
	updatePerson3(&p)
	printPerson3(&p)
}

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()
}

json [Golang] golang #golang #snippets中有用的片段

[ fmt ](https://golang.org/pkg/fmt/)
-------
Print any `type`(struct,maps,primitives) with the `key` name 

``` 
fmt.Printf("\n [key] :%+v \n", [value]) 

```

Print Error  

``` 
fmt.Errorf(" \nError: %s", err.Error()) 

```

[ log ](https://golang.org/pkg/log/)
-------------

Print any `type`(struct,maps,primitives) with the `key` name 

``` 
log.Printf("\n [key] :%+v \n", [value]) 

```


[ http  ](https://golang.org/pkg/log/)
-------------

http middleware 

```  
func [middlewareName](h http.Handler) http.Handler {
              return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
              // Do things before request 
           	
              nextMiddleware.ServeHTTP(w, r)
              // Do things after request	  
              })
          } 
```
{
         "http Middleware declaration": {
		"prefix": "md",
		"body": ["func $1(${2:nextMiddleware} http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(${3:w} http.ResponseWriter, ${4:r} *http.Request) {\n\t $5\n\t nextMiddleware.ServeHTTP(${3:w}, ${4:r})\n\t 	  $6\n\t})\n}"],
		"description": "Snippet for http middleware declaration"
	},

	"log.Printf()": {
		"prefix": "lf",
		"body": [
			"log.Printf(\"\\n## ${1:text} ##: %+v\\n\", ${2:value})"
		],
		"description": "log.Printf()"
	},
	"fmt.Printf()": {
		"prefix": "ff",
		"body": [
			"fmt.Printf(\"\\n## ${1:text} ## :%+v \\n\", ${2:value})",
		],
		"description": "log.Printf()"
	}
}

以上是关于golang Golang - Asterisk和Ampersand Cheatsheet的主要内容,如果未能解决你的问题,请参考以下文章

Golang入门到项目实战 第一个golang应用

golang编译androidso无法加载

golang如何打印内存内容

Golang入门到项目实战 golang匿名函数

json [Golang] golang #golang #snippets中有用的片段

golang使用成本高