A client sends requests to a service and uses its responses. In a habit tracker, a client calls endpoints such as CreateHabit; the API defines what to send and what response to expect. A client may also be a program calling an external service: a currency converter requests daily exchange rates from a bank endpoint. section 10.1 section 6.5.3
For a first HTTP call in Go, `http.Get(url)` uses the `net/http` package’s default client. Check the returned error: name resolution, an unavailable server, or a timeout can prevent a response. Treat data returned by an external service as untrusted, and close a successful response body when finished. section 6.5.3
As the application grows, make the client’s behavior explicit:
- Set a timeout with your own `http.Client`. The default client has no timeout, so a call may wait indefinitely; a client held by the exchange-rate component avoids changing shared global configuration. section 6.6.2
- For gRPC, give the client the server address and the service definition. Protobuf definitions can generate Go clients; a local integration test can dial a running server and call it as a real client would. section 10.1 section 10.5.2
- Choose connection credentials for the network. Disabling TLS is accepted for the restricted local test environment, not as a general production setting. section 10.5.2
Test a client across the network
Start a gRPC server on a free local port, dial its address, then call `CreateHabit` and `ListHabits` through the generated client. The integration test exercises the service through its network boundary. For an HTTP client, a separate test uses a deliberately slow test server and checks that the configured timeout produces `ErrTimeout`. section 10.5.2 section 6.6.2
Listing 10.27 integration_test.go: Creating a client
func TestIntegration(t *testing.T) {
...
// create client
habitsCli, err := newClient(t, listener.Addr().String())
require.NoError(t, err)
}
func newClient(t *testing.T, serverAddress string) (api.HabitsClient, error){
t.Helper()
creds := grpc.WithTransportCredentials(
insecure.NewCredentials()
) #1
conn, err := grpc.Dial(serverAddress, creds)
if err != nil {
return nil, err
}
return api.NewHabitsClient(conn), nil
}
The client helper takes the server address, dials it with test credentials, and returns a generated `HabitsClient`. section 10.5.2
A web server can itself become a client: after receiving a habit form, it converts the values to Protobuf and calls the backend’s `CreateHabit` endpoint. That boundary can be tested with a mocked gRPC client; input validation and user-facing errors still need deliberate design. section 11.4.2














