Remember Ryan Gosling? That pretty boy who starred as Noah Calhoun in 2004’s “The Notebook?” Well, a seemingly completely transformed actor by the same name stars in “Half Nelson,” a film directed by Ryan Fleck about a middle school teacher and cocaine addict who forms an unlikely friendship with one of his students. Gone is the bright-eyed dreamboat we came to love (and lust for) in Nicholas Sparks’ melodrama, and in his stead we are offered Dan Dunne — a weary-looking hipster with a calculator wristwatch and a week’s worth of stubble.

As a teacher, Dunne is dynamic and arguably inspiring, but at home in his apartment he becomes the troubled addict we have learned to loathe after years of DARE training. Though early on Dunne seems to be incredibly adept at managing his double life, as his shell begins to crack we cannot forget that he is immersed in the underworld he pays lip service to saving his students from.

One night after coaching a basketball game, Dunne slips into the girls’ locker room with his one-hitter, only to be discovered — incoherent, sweating and slumped on the floor — by Drey, one of his unexceptional students played by the decidedly exceptional Shareeka Epps. Epps is beautiful, strong and delicate in the ways that her inner-city surroundings and Dunne are not, and she plays Drey with a sense of complex solemnity that is comparatively rare in child actors. (Fleck and Epps have worked together once before on a short film called “Gowanus, Brooklyn,” about a young girl who investigates her teacher’s life — sounds familiar — and she is definitely a diamond in the proverbial intercity rough.)

From this locker-room encounter, we ache both for the increasingly pathetic Dunne and for the girl whose innocence is jeopardized by his reckless behavior. As the pair is drawn together by the verging desperation of their respective situations, they grow chummily conspiratorial in their world of shared secrets and rides home.

But although we always hope he will get his act together, there are times when we cannot help but hate Dunne. Perhaps we give him more leeway than we would were he played by a less swoon-worthy actor, but Fleck never lets us forget that Dunne is as much a menace to Drey’s well-being as her incarcerated brother, Mike. As he loses the moral high ground, Dunne loses our sympathy, and the film begins to threaten hopelessness.

Fortunately for the viewer’s aching heart — and pesky attention span — “Half Nelson” is punctuated by moments when things are cozy, when situations play out as you would figure, everybody is charming and the lighting is flattering. The occasional moments of deliciously sick humor help to speed up the almost-too-slow pacing and prevent the film from slipping into what would otherwise be a dismal spiral into despair.

Along with the plot being a near-masterpiece, the film’s technical aspects are well beyond competent. Fleck and cinematographer Andrij Parekh borrow the sickly color scheme, unsettling camera work and faux documentary style (and obligatory nosebleed-as-sign-of-disaster-yet-to-come scene) of Darren Aronofsky’s addiction epic, “Requiem for a Dream.”

Through careful storytelling, the filmmaker balances an incredibly gritty tale with a grand and masterfully complex scope. Dunne’s lectures on dialectics are both an unpretentiously interwoven theme in the film and obviously indicative of the troubled man’s struggle to define himself as junkie and would-be mentor (in a painfully transparent attempt to explain himself to Drey, he pleads that “one thing doesn’t make a man”).

Likewise, the inclusion of peripheral concerns — Drey’s and family friend Frank’s respective “candy” habits gesture toward an antiquated euphemism for cocaine; everybody is concerned with the injury sustained by a character we never meet named Darryl — build a story world that is both meaningful and tragic. Though Dunne’s progressive downfall is agonizingly significant to the audience, it is only one event in a world cluttered with disaster and addiction.

The film marks Fleck’s first foray into full-length drama, and one of Gosling’s first movies you don’t have to feel even a little bit guilty about loving. The promise and unabashed talent shown by both young men is reason enough to watch “Half Nelson” a second (or third, or fourth) time.