How to use pointers

use of pointers in c
what is pointer in c++
types of pointers in c++
pointers in c geeks for geeks
pointer in c example
pointers definition
list of c programs on pointers
pointer arithmetic in c

Let's say I have this function

func main() {
    x := 10
    change(&x)
}

func change(n *int) {

}

If I don't use in n *int the signature, the above function gives an error -

*cannot use &x (type int) as type int in argument to change

But why does the below example run fine without requiring a client *HTTPClient in the argument of send method though I'm passing a pointer in this case?

import (
  "net/http"
)

// HTTPClient interface for making http requests
type HTTPClient interface {
    Get(url string) (*http.Response, error)
}


func main() {
    client := &http.Client{}
    err := send(client, url)
}

func send(client HTTPClient, url string) error {
}

HTTPClient is an interface, it defines a single method Get(...)

The client struct, from http.Client also has a get method, docs here.

func (c *Client) Get(url string) (resp *Response, err error) {
    // trimmed ...
}

Source code

You can see from the definition above that Get(url) has a "pointer receiver". This means that *http.Client defines the Get(url) method and not http.Client. This means that *httpClient implements the HTTPClient interface and not http.Client.

The last thing worth pointing out is that the go runtime will automatically dereference pointers if, the an interface is implemented by a "value receiver" and not a "pointer receiver".

An example of this could be:

type Valuer interface {
    Value() string
}

type V struct {}

type (valueReceiver V) Value() string {
    // trimmed ...
}

// Here given a *V not V, go will dereference *V to call the Value() method on V not *V
vPtr := &V{}
vPtr.Value()

Pointers in C and C++, Accessing the value stored in the address using unary operator (*) which returns the value of the variable located at the address specified by its operand. (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.

http.Client has a Get method with a pointer receiver (https://golang.org/pkg/net/http/#Client.Get), having the same signature as required by your HTTPCLient interface, so it works !

C - Pointers, To use pointers in C, we must understand below two operators. To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns  One of the use cases of pointers to const elements is as function parameters: a function that takes a pointer to non-const as parameter can modify the value passed as argument, while a function that takes a pointer to const as parameter cannot.

You are implementing a HTTPClient interface which takes a function Get. In go lang spec it is defined as

func (c *Client) Get(url string) (resp *Response, err error) {
    req, err := NewRequest("GET", url, nil)
    if err != nil {
        return nil, err
    }
    return c.Do(req)
}

In above function *Client is a pointer receiver to method GET. Client is a struct declared in Go source. For more information on http.Client struct defined in net/http package Check out Golang Spec.

In your first example code you are passing a pointer to Change function.

package main

import (
    "fmt"
)

type A interface{
    Change(n *int)
} 

func main() {
    x := 10
    Change(&x)
}

func Change(n *int) {
    fmt.Println(*n+1)
}

Check working example on Go playground

Pointers - C++ Tutorials, How to Use Pointers? There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) assign  Pointers are used (in the C language) in three different ways: To create dynamic data structures. To pass and handle variable parameters passed to functions. To access information stored in arrays. (Especially if you work with links).

C Tutorial – How to use Pointers » CodingUnit Programming Tutorials, A bit later, we will see how to declare and use pointers. Dereference operator (*). As just seen, a variable which stores the address of another variable is called a  Exercise 4: Copy the source code from A Pointer Announces a String in your editor. Build and run. The boatload of caution in A Pointer Announces a String, and anytime you use a pointer to directly declare a string, is that the pointer variable can’t be manipulated or else the string is lost. For example,

C Pointers (With Examples), To make full use of the C Programming language, you have to have a very good understanding of pointers. For most people it will take some time to fully  Initially, the address of c is assigned to the pc pointer using pc = &c;. Since c is 5, *pc gives us 5. Then, the address of d is assigned to the pc pointer using pc = &d; .

Pointers in C language with examples, In this tutorial, you'll learn about pointers; what pointers are, how do you use them and the common mistakes you might face when working with them with the  Create a pointer variable with the name ptr, that points to a string variable, by using the asterisk sign * (string* ptr). Note that the type of the pointer has to match the type of the variable you're working with. Use the & operator to store the memory address of the variable called food, and assign it to the pointer.