Skip to content

Instantly share code, notes, and snippets.

@esammer
Last active June 29, 2021 01:24
Show Gist options
  • Save esammer/e7cdb3fc5fd0563aefbd7c751813e096 to your computer and use it in GitHub Desktop.
Save esammer/e7cdb3fc5fd0563aefbd7c751813e096 to your computer and use it in GitHub Desktop.
A clock interface for Go.
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
// I'm literally begging you to use this code.
// Clock is something that knows what time it is.
//
// This interface allows mocking of internal functions that rely on time. In all real world use cases, you probably
// want WallClock.
type Clock interface {
// Now returns the current time according to the clock.
Now() time.Time
}
// WallClock is a Clock that returns the actual wall clock time.
//
// This just adapts time.Now() to meet the Clock interface.
type WallClock struct{}
func (*WallClock) Now() time.Time {
return time.Now()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment