When the Irish writer Nuala O'Faolain discovered she had only a few weeks left to live, a terrible doubt entered her mind.

Did it matter: all the knowledge inside her head? All the books she had read? In particular, she began to doubt the point of reading novels. She was about to die, she thought, so why on earth should she waste any more time on things that were made up?

It's hard to think of a more profound challenge than that one for a novelist, someone who spends their whole life making things up, but it's how O'Faolain felt as death approached. According to her friend and fellow writer Hugo Hamilton, O'Faolain believed the goodness had gone out of everything. "I tried to read Proust again," she said at one point, "but the magic has gone. It amazed me how quickly my life turned black."

Ah, but it wasn't all black, as we discover in Every Single Minute, an elegant little book Hamilton has written about his friendship with O'Faolain and her final few weeks. The book starts in 2008 as O'Faolain talks about her last wish to go abroad and when she finally decides on Berlin, we discover that her disavowal of novels and the world of made-up is not as clear as it first appears.

"She said what she said about novels categorically," remembers Hamilton, speaking by phone from his home in Dublin, "and then a week later, when I was taking her to Berlin, she began to revise that view so although she still believed it, that all those books were a waste of time, she still wanted more. That's the contradiction in life. When we come to death, we think: what's it about? Was this a waste of time? And yet we still want more of it. On my deathbed, I won't regret the novels I've read. The regret will be losing all of them."

Hamilton develops this idea - that stories matter - all the way through Every Single Minute, but considering what his friend said about novels in her last days, the way he has chosen to do it is interesting: instead of writing a memoir, Hamilton has chosen to fictionalise the trip to Berlin. In other words, he has turned the story into the very thing Nuala O'Faolain was railing against. He has turned it into a novel.

A memoir might have seemed like a more obvious choice for Hamilton, not least because he is famous for his own memoir, The Speckled People (2003), which relates his eccentric and troubling childhood in Dublin. The son of a German father and Irish mother, he was only allowed to speak Irish or German as he was growing up, which led to a profound alienation not only from his parents but also from the society he was living in.

In planning Every Single Minute, Hamilton could have used the memoir format again but he settled on the novel, he says, because it meant he would feel less boxed in by the facts. O'Faolain was well-known for her own memoirs, which meant the facts of her life were already out there. What Hamilton felt he could do with a novelised version was investigate beyond the facts. And anyway, he's not convinced the boundaries between novel and memoir are as solid as we think they are.

"For me, neither memoir nor fiction is about photo realism - it's much more a question of looking beyond the facts," he states. "A certain amount of memoir writing is about reimagining the past - having written a memoir myself, I know that - and what fiction does is it investigates beyond the facts. It could almost be described as much closer to the truth. Think of Lucian Freud. His paintings weren't photographs but sometimes they're far more interesting and sometimes more true. Freud got inside the person when he painted."

Hamilton says he had the same aim in writing Every Single Minute - to get to the person inside O'Faolain and inside himself, but also to get inside the subject of death. When the two writers made the trip to Berlin, O'Faolain had little over a week left to live and Hamilton describes the small details of that - the little bag she carried round with her in her wheelchair, the medication - and also describes the bigger feelings; what it felt like to be right up against death. "It was like extra time," he writes in the book. "It helped us to forget what was happening to her ... as long as we kept moving and telling each other stories, as long as the streets were going by."

This illusion of extra time was a coping mechanism, of course, just like all the other coping mechanisms on death - denial, doctors, religion - but Hamilton also discovered that he was exploring the specific relationship between books and death. O'Faolain may have believed death destroys books but Hamilton discovered it could be the other way around: books can destroy death. "It's part of the human condition that you cannot live without stories," he says, "and I make that point in the book, through Nuala, that basically we are stories. We exist physically but I think she discovers we are the story that we tell about ourselves or what's told about us."

In writing the book, Hamilton, who is in his sixties, also found telling the story was a way of dealing with his own fear of mortality. "I was terribly conscious and afraid of death," he admits. "But this journey with Nuala has released me from that fear. I think it's possible that writers have a gift of being able to step outside life into the imagination - the terror of death is probably still there in me but I kind of sidestepped it through writing. I've always had that luxury in a way that writers have of writing my way to a solution. Even when you're poor, for example, you don't feel it as much because you can go and write."

What Hamilton certainly does not need anymore when dealing with the subject of death is religion. As he described in The Speckled People, both his parents were Catholic, and extreme ones too, but he worked out early on that he was an atheist. Writing The Speckled People also helped him discover what O'Faolain called the rhythm of honesty - the soothing effect that being truthful has, particularly being truthful about the past.

"It's true in my own life that I refused to remember," says Hamilton. "I had buried my entire memory. I had erased my childhood and that was because of not being allowed to speak English - it was a mental brutality. We were not able to speak English at home and were hounded for speaking German on the street. That kind of alienation from the outside world was terribly difficult."

Hamilton deals with some of this alienation in Every Single Minute. "The book has opened up a lot of things," he says. "It is this blurred portrait of myself and Nuala but I think I've also discovered a way of engaging with my father that I hadn't before. I was always trying to understand what he was trying to do in his misguided way, but in this book I've found a way of liking him for certain things."

Which means Every Single Minute, which is ostensibly about death, is really about life. "Absolutely," says Hamilton. "All books about death are actually about life."

Every Single Minute is published by Fourth Estate at £14.99